• Resolved cvc1968

    (@cvc1968)


    I have a post_meta field called pe_theme_meta. Here is an example value:

    O:8:"stdClass":1:{s:4:"info";O:8:"stdClass":1:{s:8:"position";s:9:"President";}}

    When I try to call this field in the [loop] it results in a white screen of death for that page.

    The only thing I really need from that field is “President”.

    Is there any way to get “President” to display?

    Thank you.

    https://www.ads-software.com/plugins/custom-content-shortcode/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter cvc1968

    (@cvc1968)

    Correction.

    The white screen wasn’t a typical WordPress white screen. It was the result of a failure of javascript to fade out a white loader.

    Here are the error messages I found under the white layer:

    Warning: strpos() expects parameter 1 to be string, object given in /home/imagea8/public_html/msaanetwork/wp-includes/shortcodes.php on line 189

    Catchable fatal error: Object of class stdClass could not be converted to string in /home/imagea8/public_html/msaanetwork/wp-includes/shortcodes.php on line 197

    Plugin Author Eliot Akira

    (@miyarakira)

    Thank you for telling me about this condition. In the latest plugin update, I added a parameter to get values from an object stored in a field.

    In the above case, this should display what you need:

    [field pe_theme_meta property="position"]

    This feature is untested right now. Could you tell me if it works?

    Thread Starter cvc1968

    (@cvc1968)

    Sorry for taking so long to get back to this.

    That shortcode doesn’t work, but I’m not getting any errors. Just nothing.

    For the record, I worked around this problem in the following way:

    I installed Advanced Custom Fields and created a field for this post type and one other post type called ‘utility’. I set the field to appear in a panel on the edit screen.

    In functions.php I enqueued a script that loads into Admin using the hook for post edit screens only:

    function my_load_scripts($hook) {
    	if( $hook != 'edit.php' && $hook != 'post.php' && $hook != 'post-new.php' )
    		return;
    	wp_enqueue_script( 'autofill-js', get_stylesheet_directory_uri() . '/js/autofill_fields.js', array('jquery'), '1.0.0', true );
    }
    add_action('admin_enqueue_scripts', 'my_load_scripts');

    The script ‘autofill_fields.js’ contains the following, which can probably be done more efficiently than this:

    jQuery(document).ready(function(){
           // determine which post type we're dealing with by identifying which field is present
    	if (jQuery('#pe_theme_meta_info__position_').length > 0) {
    		var pval = jQuery('#pe_theme_meta_info__position_').val();
    	} else if (jQuery('#pe_theme_meta_info__type_').length > 0) {
    		var pval = jQuery('#pe_theme_meta_info__type_').val();
    	}
           // now copy the value of the relevant field to my 'utility' field
    	jQuery('#acf-field-utility').val(pval);
    
            // If the field changes, change 'utility' along with it
    	jQuery('#pe_theme_meta_info__position_').change(function(){
    		var pval = jQuery('#pe_theme_meta_info__position_').val();
    		jQuery('#acf-field-utility').val(pval);
    	});
    
            // And this is the same for the other post type
    	jQuery('#pe_theme_meta_info__type_').change(function(){
    		var pval = jQuery('#pe_theme_meta_info__type_').val();
    		jQuery('#acf-field-utility').val(pval);
    	});
    });

    So now, whatever is in the Position field gets written to the utility field and I use
    [field utility] to display that info.

    Plugin Author Eliot Akira

    (@miyarakira)

    Woo, that is quite a solution! I like that it’s automated for all applicable posts.

    I see, so the new shortcode parameter didn’t do what I expected. It’s difficult to know how to extract the data from this field, since I can’t see or test it on my end. Probably what was needed was something like:

    [field pe_theme_meta property="info->position"]

    ..but that functionality doesn’t exist yet, to access sub-levels of object properties.

    Anyway, it seems to be specific to the theme and how it stores its data, so perhaps this function is not necessary. Looks like you’ve found a solution to get the data you need.

    Thread Starter cvc1968

    (@cvc1968)

    Thanks for your efforts, Eliot. JSON still confounds me, to be honest. It probably is too theme-specific in this case. Anyway, everything is still working fine, so we’re good.

    Gonna mark this resolved.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Get a value from field containing json’ is closed to new replies.