Shortcode in customfield soes not show up in results

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Shortcode in customfield soes not show up in results

This topic contains 17 replies, has 2 voices, and was last updated by

 
Keymaster
2 years, 10 months ago.

Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #21923

    Participant

    I am using custom fields in my search page ( Search and Filter. )
    Every team on my test site has a monthly price. i use the plugin “Shortcoder” to show al the different prices on the different pages. So if a price changes I only have to change the shortcode.

    Now my problem/question is as follows. Now I have the monthly price as plain text in the custom field “pricemonth” That works fine in the results page.

    BUT I want to use the monthly price shortcode in my customfield. Now I know that the default custom fields meta box provided by WordPress strips out shortcodes from custom field values. This means you cannot, by default, use shortcodes within the values of your custom fields.

    I tried several solutions, for in stance I placed” echo do_shortcode( get_post_meta( get_the_id(), ‘pricemonth’, true ) ); in the functions.php of my child theme. I tried several others code, like ” <?php echo do_shortcode(get_post_meta(get_the_ID(), ‘pricemonth’, true)); ?>”

    I cannot get it to work in the resultspage. The shortcode is not executed, it just shows the plain text of the shortcode.

    Have you any idea how to get this to work ??

    • This topic was modified 2 years, 10 months ago by  ckworld.
    • This topic was modified 2 years, 10 months ago by  ckworld.
    #21926

    Participant

    I tried this in the advanced description field:

    <div class=”fusion-button-wrapper fusion-alignright”><span class=”fusion-button-text”>Details & Reserve</span></div>
    <?php
    $cf_value = get_post_meta(get_the_ID(), ‘pricemonth’, TRUE);
    echo do_shortcode( $cf_value );
    ?>

    Price / month {pricemonth}<br><br>
    {teamlist}<br>

    Did not work

    #21931

    Hi,

    Well, the advanced description field is not going to work in this case, it does not support PHP code for security reasons.

    The only possibility is via custom code via the theme functions.php file. However it is still possible, that the shortcode might not be registered when the ajax request is finished, although in that case I think it should return an empty string, and not the shortcode name.

    One thing to note, that the do_shortcode(..) function requires the shortcode to be in brackets. Anothe rissue I noticed in the code, is that the get_the_ID() function will not work there, as the ajax reqults are out of the wordpress loop.

    Try this custom code, although I don’t know if this is going to work in any way, as I don’t know the exact content of the custom fields:

    add_filter( 'asp_results', 'asp_custom_field_to_results', 10, 1 ); 
    function asp_custom_field_to_results( $results ) {
      $custom_field = 'pricemonth'; // Enter the custom field name
      $field = 'content';               // 'title' or 'content'
      $position = 'before';           // 'before' or 'after'
      $delimiter = ' ';               // character between the field value and the field
    
      foreach ($results as $k=>&$r) {
        if ($r->content_type != 'pagepost') continue;
        if ( function_exists('get_field') )
            $meta_value = get_field( $r->id, $custom_field, true ); // ACF support
        else
            $meta_value = get_post_meta( $r->id, $custom_field, true );
        $meta_value = do_shortcode($meta_value);  
        // Modify the post title to add the meta value
        if ( !empty($meta_value) ) {
          if ( $field == 'title' ) {
            if ( $position == 'before' )
              $r->title  = $meta_value . $delimiter . $r->title;
            else
              $r->title  .= $delimiter . $meta_value;
          } else {
            if ( $position == 'before' )
              $r->content  = $meta_value . $delimiter . $r->content;
            else
              $r->content  .= $delimiter . $meta_value;
          }
        }
      }
     
      return $results;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #21933

    Participant

    I put the code in the .functions.php of my child theme. Unfortunately it did not work. The results page just shows the shortcode ( [sc=name=”pricemonth”] )in plain txt, see printscreen

    Attachments:
    You must be logged in to view attached files.
    #21935

    Participant

    See printscreen, this is the custom field in the WordPress page with the name of the custom field and the shortcode (between brackets)

    Attachments:
    You must be logged in to view attached files.
    #21937

    Participant

    I do not understand why this is not working, because all the other shortcodes in the pages render fine.

    #21938

    Hi,

    I honestly don’t know either. The most likely cause is, that the shortcode is not yet registered at the time of the execution. The do_shortcode() call is there, so it should work, if it exists.
    Maybe this shortcode is not registered within ajax requests, or only registered during certain conditions.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #21942

    Participant

    Is there a file in the Ajax Search Pro plugin itself to place the code, so it might work??

    #21943

    Hi,

    Well, there are template files, for more information you can read this. In you case, you are looking for the vertical.php file.

    However, those are still called within the ajax request, so there is basically no difference using the shortcode within a custom code, or within these files. You can try, but if the shortcode is not registered, then it won’t make any difference.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #21946

    Participant

    FIXED it, shortcodes are now working (with your php code ) in the custom fields !!!!!

    #21947

    Participant

    I just had to remove {pricemonth} from the Advanced Description Field.

    Two more questions:
    1.] The shortcode shows fine, but now I have no control over where it shows up in the results box. I am fine with the placement now, but interested to know how to let it show elsewhere, for instance right in the box etc.

    2.] I would like to put another shortcode ( availability ) in an other customfield. I tried the same code you provided with another extra custom field name, that does not work.

    I tried this, but then it only shows the second customfield
    —–
    add_filter( ‘asp_results’, ‘asp_custom_field_to_results’, 10, 1 );
    function asp_custom_field_to_results( $results ) {
    $custom_field = ‘pricemonth’; // Enter the first custom field name
    $custom_field = ‘availability’; // Enter the second custom field name

    $field = ‘content’; // ‘title’ or ‘content’
    $position = ‘before’; // ‘before’ or ‘after’
    $delimiter = ‘ ‘; // character between the field value and the field

    foreach ($results as $k=>&$r) {
    if ($r->content_type != ‘pagepost’) continue;
    if ( function_exists(‘get_field’) )
    $meta_value = get_field( $r->id, $custom_field, true ); // ACF support
    else
    $meta_value = get_post_meta( $r->id, $custom_field, true );
    $meta_value = do_shortcode($meta_value);
    // Modify the post title to add the meta value
    if ( !empty($meta_value) ) {
    if ( $field == ‘title’ ) {
    if ( $position == ‘before’ )
    $r->title = $meta_value . $delimiter . $r->title;
    else
    $r->title .= $delimiter . $meta_value;
    } else {
    if ( $position == ‘before’ )
    $r->content = $meta_value . $delimiter . $r->content;
    else
    $r->content .= $delimiter . $meta_value;
    }
    }
    }

    return $results;

    }

    ————–

    • This reply was modified 2 years, 10 months ago by  ckworld.
    #21952

    Hi,

    1. Because this is a custom code, the value can be inserted either before or after the whole content/title fields. You can define that by changing the $position and $field variables according to the comments after them.

    2. This code only works with a single field. Try this variation, with this, enter the field names to the $custom_field variable, as a comma separated list:

    add_filter( 'asp_results', 'asp_custom_field_to_results', 10, 1 ); 
    function asp_custom_field_to_results( $results ) {
      $custom_field = 'pricemonth, availability'; // Enter the custom field name
      $field = 'content';               // 'title' or 'content'
      $position = 'before';           // 'before' or 'after'
      $delimiter = ' ';               // character between the field value and the field
    
      // -- Do not change anything below --
      $fields = explode(',', $custom_field);
      foreach ($results as $k=>&$r) {
        if ($r->content_type != 'pagepost') continue;
        
        foreach ( $fields as $cfield ) {
          $cfield = trim($cfield);
          if ( $cfield == '' )
            continue;
          if ( function_exists('get_field') )
              $meta_value = get_field( $r->id, $cfield, true ); // ACF support
          else
              $meta_value = get_post_meta( $r->id, $cfield, true );
          $meta_value = do_shortcode($meta_value);  
          // Modify the post title to add the meta value
          if ( !empty($meta_value) ) {
            if ( $field == 'title' ) {
              if ( $position == 'before' )
                $r->title  = $meta_value . $delimiter . $r->title;
              else
                $r->title  .= $delimiter . $meta_value;
            } else {
              if ( $position == 'before' )
                $r->content  = $meta_value . $delimiter . $r->content;
              else
                $r->content  .= $delimiter . $meta_value;
            }
          }
        }
    
      }
     
      return $results;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #21953

    Participant

    No that does not work, neither of the shortcodes show up.

    I think I need an extra (different) code for the second custom field

    • This reply was modified 2 years, 10 months ago by  ckworld.
    #21955

    Participant

    SORRY, IT DID WORK, just mistyped something.

    Now how can I style the custom field results, for instance bold or difefrent color

    #21959

    That highly depends on the output HTML. You could potentially change this line:

    $meta_value = do_shortcode($meta_value);

    ..to maybe:

    $meta_value = '<span class="aspcf_'.$cfield.'">' . do_shortcode($meta_value) . '</span>';

    With that, the shortcode values will be printed into a span element with the classes ‘aspcf_pricemonth’ and ‘aspcf_availability’. Now, you can use custom CSS to style them as you want, for example:

    span.aspcf_pricemonth {
        font-weight: bold;
        color: red;
    }
    
    span.aspcf_availability {
        font-weight: bold;
        color: blue;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 15 posts - 1 through 15 (of 18 total)

You must be logged in to reply to this topic.