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 Ernest Marcinko 4 years, 2 months ago.
- AuthorPosts
- March 31, 2019 at 1:53 pm #21923
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 ??
March 31, 2019 at 3:08 pm #21926I 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
April 1, 2019 at 10:17 am #21931Hi,
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 theget_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:
Best,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; }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
April 1, 2019 at 11:12 am #21933I 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.April 1, 2019 at 11:16 am #21935See 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.April 1, 2019 at 12:17 pm #21937I do not understand why this is not working, because all the other shortcodes in the pages render fine.
April 1, 2019 at 12:39 pm #21938Hi,
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
Best,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.
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
April 1, 2019 at 3:03 pm #21942Is there a file in the Ajax Search Pro plugin itself to place the code, so it might work??
April 1, 2019 at 3:06 pm #21943Hi,
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 :)
April 1, 2019 at 9:06 pm #21946FIXED it, shortcodes are now working (with your php code ) in the custom fields !!!!!
April 2, 2019 at 7:18 am #21947I 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 fieldforeach ($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 4 years, 2 months ago by
ckworld.
April 2, 2019 at 10:19 am #21952Hi,
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:
Best,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; }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
April 2, 2019 at 10:31 am #21953No 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 4 years, 2 months ago by
ckworld.
April 2, 2019 at 10:34 am #21955SORRY, IT DID WORK, just mistyped something.
Now how can I style the custom field results, for instance bold or difefrent color
April 2, 2019 at 2:39 pm #21959That 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:
Best,span.aspcf_pricemonth { font-weight: bold; color: red; } span.aspcf_availability { font-weight: bold; color: blue; }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
-
This reply was modified 4 years, 2 months ago by
- AuthorPosts
You must be logged in to reply to this topic.