• Resolved katzw

    (@katzw)


    Hi! I have a question about whether or not the function below should work – it’s loaded into the functions.php file, and the idea would be that it calls a custom field (which is successfully called elsewhere) into an infobox on a map. Here is what the plugin author starts with:

    function cspm_custom_infobox_description($default_description, $post_id){
    	$custom_content = "YOUR CUSTOM CONTENT HERE.";
    	return (!empty($custom_content)) ? $custom_content : $default_description;
    }
    add_filter("cspm_custom_infobox_description", "cspm_custom_infobox_description", 10, 2);
    add_filter("cspm_large_infobox_content", "cspm_custom_infobox_description", 10, 2);

    And this is what I would like to work:

    function cspm_custom_infobox_description($default_description, $post_id){
    	$custom_content = "<?php get_cfc_field('locations','web-address');" ;
    	return (!empty($custom_content)) ? $custom_content : $default_description;
    }
    add_filter("cspm_custom_infobox_description", "cspm_custom_infobox_description", 10, 2);
    add_filter("cspm_large_infobox_content", "cspm_custom_infobox_description", 10, 2);

    So, essentially just dropping in a call to the function, which when I use it in html works fine, but here it’s a PHP variable. Can anyone please tell me what I’m doing wrong here?

Viewing 7 replies - 1 through 7 (of 7 total)
  • A quick search on get_cfc_field() says that it accepts that 3rd parameter of $post_id, otherwise it uses the global $post ( as far as I can tell ). I would modify it into something like this:

    function cspm_custom_infobox_description( $default_description = '', $post_id ) {
    	global $post;
    	
    	$post_id 	= ( ! empty( $post_id ) ) ? $post_id : $post->ID;
    	$custom_content = get_cfc_field( 'locations', 'web-address' );
    	return ( ! empty( $custom_content ) ) ? $custom_content : $default_description;
    }
    add_filter("cspm_custom_infobox_description", "cspm_custom_infobox_description", 10, 2);
    add_filter("cspm_large_infobox_content", "cspm_custom_infobox_description", 10, 2);

    The above breaks your PHP function call to get_cfc_field() out of a string and actually called like a PHP function. The line above it will use the global $post ID if one isn’t passed to it.

    • This reply was modified 7 years, 8 months ago by Howdy_McGee.
    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    The error is here:

    $custom_content = "<?php get_cfc_field('locations','web-address');" ;

    As it’s already in”php mode”, you’d just do

    $custom_content = get_cfc_field('locations','web-address');

    Thread Starter katzw

    (@katzw)

    Hi guys – thanks so much for the replies:

    Howdy: I tried your code, and while it looked good to me, it seemed to skip it, and only generate the_content portion of the post inside the infobox.
    Steven: Ok – I had tried it your way earlier – so at least I know I wasn’t wrong at the outset.

    Unfortunately neither work.
    I’ve tried incorporating the post_id per WCK’s documentation:
    function cspm_custom_infobox_description($default_description, $post_id){

    $custom_content = the_cfc_field(‘locations’,’web-address’, $post_id = false, $key = 0, $do_echo = true );

    return (!empty($custom_content)) ? $custom_content : $default_description;

    (also with the variation get_cfc_field) to no avail.
    I’m able to use these fields in page templates, why not in this case? Any ideas? Thanks again!

    @katzq With my code try adding in $post_id as the 3rd parameter – that missing parameter may be why it isn’t returning anything and I forgot to add that in the original post.

    
    function cspm_custom_infobox_description( $default_description = '', $post_id ) {
    	global $post;
    	
    	$post_id 	= ( ! empty( $post_id ) ) ? $post_id : $post->ID;
    	$custom_content = get_cfc_field( 'locations', 'web-address', $post_id );
    	return ( ! empty( $custom_content ) ) ? $custom_content : $default_description;
    }
    add_filter("cspm_custom_infobox_description", "cspm_custom_infobox_description", 10, 2);
    add_filter("cspm_large_infobox_content", "cspm_custom_infobox_description", 10, 2);
    
    Thread Starter katzw

    (@katzw)

    Hello Howdy!

    It worked!! Thank you so, so much! There’s one last piece of the puzzle, I’m hoping you can point in the right direction:

    The code returns the web address, but ideally it would be a live link. If I take the return, which custom_content has now passed to default_description, could I then echo:

    echo '<a>"><?php $default_description ?></a>'?

    Also, how difficult would it be to bring in other custom fields – could I run a function for each (Address / Phone number?)

    Thank you again – it’s so appreciated!!

    Moderator bcworkz

    (@bcworkz)

    The value for $default_description is passed to your filter callback by the apply_filters() call managing the info box content. I’m not sure where you plan on echoing $default_description but it likely would not work as you expect. Most likely $default_description would be out of scope. It is only valid within the callback. If you were to echo it out within the callback, it’ll probably end up in the wrong place or not end up anywhere visible.

    Echoing out within a filter callback is generally a bad practice. The purpose of filters is to change content used by the calling code, not generate output.

    If the other fields have similar filters, then you can use a similar concept to filter the resulting output for those fields. Mostly you would change the filter added to and the CFC field to get.

    Thread Starter katzw

    (@katzw)

    I’m going to make this as resolved ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Plugin function to Call WCK Custom Field not Working’ is closed to new replies.