Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

how to get shortcodes to work inside the text of custom fields WordPress

  • SOLVED

I am building a learning management system using Wordpress, custom fields, and various plugins, especially SIMPLE FIELDS by Par Thernstrom. My shortcode plugins, such as Shortcode Exec PHP and Custom Field Value, work fine in the main editing box, but if I put any of my course information shortcodes into a custom field, they don't work in the custom field. I know that prevents "circular reference" problems, but it also prevents me form building a more streamlined system. Do you know of any workarounds that would allow plugins to operate on the contents of custom fields, so that custom fields could contain shortcodes? The specific configuration that I most need is for shortcodes from Shortcode Exec PHP to work on the wysiwyg textarea input boxes of SIMPLE FIELDS.

Answers (7)

2011-11-11

Clifford P answers:

Have you tried any/all of these suggested answers? Your response to each one seems to be copied and pasted.


dennisrivers comments:

Thanks for your response, Cliff.

Most of the responders do not seem to really understand my question. I am not trying to put new php into the page template. I am trying to put a shortcode such as [course-teacher] into the text of a custom field. I did try one of the suggested answers, but it seems to me that everyone is assuming that I am working on the template.

The SIMPLE FIELDS plugin that manages custom fields quite well, does not come with a built-in shortcode scheme for inserting custom field values into the middle of a post or page, so I had to cook one up using Shortcode Exec PHP. it works fine in the main edit window, but not in the text of another custom field.

Am I correct when I say that "the custom field text managed by the SIMPLE FIELDS plugin is not evaluated by Wordpress for the presence of shortcodes of any sort, or for the presence of PHP code, as far as I can see from my experiments" ?

Thanks for your help.


Clifford P comments:

So what you're wanting is shortcodes to work in custom fields, instead of displaying in plain text.
Do you want ALL/ANY shortcodes to work in custom fields, or just the specified ones (e.g. show-reading-1)?

What is in the shortcode's content, just a block of text/html, or php or more shortcodes?

FYI: you could use [[LINK href="http://wpstorm.net/wordpress-plugins/post-snippets/"]]http://wpstorm.net/wordpress-plugins/post-snippets/[[/LINK]] to store non-php snippers. It creares shortcodes for you (1plugin instead of your 2). That page's download link takes you to WP Plugins Repository page.


dennisrivers comments:

Thank you so much for the above suggestion. I have just voted to award you half the prize.

This is a helpful, but not complete, answer to my question. The POST SNIPPETS plugin allows for very simple short codes, such as [teacher-name], that would be more understandable for most members of the project team.

The limitation of this suggestion are:

A. The POST SNIPPETS plugin creates site-wide snippets, but not page-specific snippets, for which I have a large need. (The SNIPLETS plugin does both, but its shortcodes are not as clean as those allowed by POST SNIPPETS.) So POST SNIPPETS is the best choice for editor-friendly site-wide text blocks.

B. The POST SNIPPETS plugin will not work on the content of the SIMPLE FIELDS plugin (that allows wysiwyg textarea custom fields) until you implement the suggestion below by Romel Apuya, of adding the following line to the theme's function file:

add_filter('the_content', 'do_shortcode');

Thanks again for you help.

2011-11-11

Romel Apuya answers:

hi i think the best thing you can do is
open your functions.php and add this short line

add_filter('the_content', 'do_shortcode');


then save.

thanks,


dennisrivers comments:

Thank you so much for the above suggestion. I have just voted to award you half the prize.

Your suggestion was valuable to me because it allowed all my installed plugins to work on the content of the wysywig textarea fields managed by the SIMPLE FIELDS custom field manager plugin.

Because the net effect of what I am doing is to include the content of one custom field on a given page inside the body of another custom field on that same page, I still may need to solve "order of execution" problems. I will also need to be very careful to avoid circular references.

Your solution works great for inserting site-wide text blocks managed by POST SNIPPETS into the custom fields of a specific page or post, using easy-to-read shortcodes (Cliff P's suggestion above, which needed your part to work with SIMPLE FIELDS).

Thanks again for your help.

2011-11-11

Kailey Lampert answers:

How are you outputting the custom fields?

Usually you can just wrap do_shortcode() around it. Something like:


do_shortcode( get_post_meta( $post->ID, 'custom_field', true ) );


dennisrivers comments:

I am inserting the custom fields at various locations in a page by using a few lines of php in the SHORTCODE EXEC PHP plugin.

These lines named "show-reading-1" in SHORTCODE EXEC PHP:

$tempvariable = simple_fields_get_post_value(get_the_ID(), "Reading Assignment from Book 1", true);
echo "$tempvariable";

will allow me to put [show-reading-1] anywhere on the page where I might need it. I am working on the page, not the template. This scheme works fine inside the main edit window, but not inside custom fields.

My question is how to get shortcodes to work inside the text of custom fields.


Kailey Lampert comments:

So you want to put '[show-reading-1]' inside a custom field, to be called by another shortcode (forgive me, recursion gets the best of me sometimes).

What about this
$tempvariable = do_shortcode( simple_fields_get_post_value(get_the_ID(), "Reading Assignment from Book 1", true) );
echo "$tempvariable";


dennisrivers comments:

the problem is that right now, the custom field text is not evaluated by Wordpress for the presence of shortcodes of any sort, as far as I can see from my experiments. Just as you might want to put shortcodes for teacher-name or text-book1 in the main edit box, one might also want to insert such local or global data strings into one or more custom fields.


Kailey Lampert comments:

I guess I don't understand exactly.

Whether in Shortcode Exec PHP or in your template, you can use do_shortcode() to make WordPress handle any shortcodes found in the content passed to it.


dennisrivers comments:

There are a variety of text replacement plugins available, and so far as I can see they all work fine in the main edit window or a page or post, and none of them work if inserted into the text of a custom field.

2011-11-11

Utkarsh Kukreti answers:

How are you fetching the values on the frontend? Something like this should work:

<?php $var = get_post_meta(1, 'foo', true);
echo do_shortcode($var);


dennisrivers comments:

I am inserting the custom fields at various locations in a page by using a few lines of php in the SHORTCODE EXEC PHP plugin.

These lines named "show-reading-1" in SHORTCODE EXEC PHP:

$tempvariable = simple_fields_get_post_value(get_the_ID(), "Reading Assignment from Book 1", true);
echo "$tempvariable";

will allow me to put [show-reading-1] anywhere on the page where I might need it. I am working on the page, not the template. This scheme works fine inside the main edit window, but not inside custom fields.

My question is how to get shortcodes to work inside the text of custom fields.

2011-11-11

Pau answers:

simply add do_shortcode in the output function.

ex.

do_shortcode( get_post_meta( $post->ID, 'field_name', true ) );


dennisrivers comments:

I am inserting the custom fields at various locations in a page by using a few lines of php in the SHORTCODE EXEC PHP plugin.

These lines named "show-reading-1" in SHORTCODE EXEC PHP:

$tempvariable = simple_fields_get_post_value(get_the_ID(), "Reading Assignment from Book 1", true);
echo "$tempvariable";

will allow me to put [show-reading-1] anywhere on the page where I might need it. I am working on the page, not the template. This scheme works fine inside the main edit window, but not inside custom fields.

My question is how to get shortcodes to work inside the text of custom fields.

2011-11-11

Luis Cordova answers:

are you saying from the admin dashboard you are trying to edit say a post and there on the custom field you are trying to insert shortcodes and get them parsed?

if so then I would suggest to insert the do_shortcode hook but into the hook that reads the custom field edition... hmm can you please confirm?

thanks


dennisrivers comments:

Thank you for your response.

The problem is that right now, the custom field text managed by the SIMPLE FIELDS plugin is not evaluated by Wordpress for the presence of shortcodes of any sort, or for the presence of PHP code, as far as I can see from my experiments. Just as you might want to put shortcodes for teacher-name or text-book1 in the main edit box, one might also want to insert such local or global data strings into one or more custom fields.

There are a variety of text replacement plugins available, and so far as I can see they all work fine in the main edit window or a page or post, and none of them work if inserted into the text of a custom field.


2011-11-11

Just Me answers:

Not sure what it looks like exactly on your site but let's say your custom field name is "MyField". And you want to use [show-reading-1] inside MyField. Then this will probably work.



<?php
echo do_shortcode('[show-reading-1]');
?>


You will need to put all the code (starting at <?php and ending at ?>) in your custom field MyField.


dennisrivers comments:

Thank you for your response.

The problem is that right now, the custom field text managed by the SIMPLE FIELDS plugin is not evaluated by Wordpress for the presence of shortcodes of any sort, or for the presence of PHP code, as far as I can see from my experiments. Just as you might want to put shortcodes for teacher-name or text-book1 in the main edit box, one might also want to insert such local or global data strings into one or more custom fields.

I tried your solution, but it did not work. At this point, Wordpress is not looking for blocks of php code in the text held in a textarea custom field.


dennisrivers comments:

There are a variety of text replacement plugins available, and so far as I can see they all work fine in the main edit window or a page or post, and none of them work if inserted into the text of a custom field.