I am creating a WordPress plugin where a user can specify a custom loop repeater based on the value of a textarea.
The idea is, a user can use a predefined repeater that ships with the plugin or they can build their own repeater by simply adding html + PHP to the textarea.
Example of a custom repeater:
<h1><?php the_title(); ?></h1> <?php the_excerpt(); ?> - <?php the_time(); ?>
The issue is when I echo/print the textarea value, the WordPress functions within the <?php ?> tags do not execute.
Is what I'm attempting even possible?
Let me explain further...
I am building an installable plugin based on my Ajax Load More script.
This plugin will be only used by site admins and for total control of the display of posts I want to allow them to create a custom repeater.
On the plugin settings page, I want to have a textarea where the admins can add whatever html/php code they want in order to customize the repeater.
My issue is how do I execute the PHP entered within the textarea on the frontend as echoing the value does not work.
Use eval function to change plain text to php code. More about it here
eval('echo "Hello world!"');
will print out "Hello world!" instead of 'echo "Hello world!"'
BUT what if i write to textarea:
die();
One way of doing this is to use the eval() function as Justinas says, but I would strongly recommend you don't use it as I have found it to be unreliable and problematic.
What I would recommend is to write the PHP code in the string you extract from the text area to a file and then include that file where you want the PHP to execute.
So, I would recommend running
<?php file_put_contents($file_path, $contents); ?>
whenever the text area is updated to update the file, then
<?php include_once($file_path); ?>
where you want the code on the front end.
Those wordpress functions will not work unless they are in the Loop, Here is a basic Loop:
<?php if(have_posts()) { ?>
<?php while(have_posts()) { ?>
<?php the_post(); ?>
<?php // custom post content code for title, excerpt and featured image ?>
<?php } // end while ?>
<?php } // end if ?>
Related
I have a php script that reads a text file of 10000 urls one in each line. The script displays all the urls in a blog post. I want the page to be divided into 10 small pages to facilitate comfortabe browsing. So how to add a pagebreak using <?php wp_link_pages(); ?> function inside a post? Something like this:
<?php
echo "Hi, This is First Page";
wp_link_pages();
echo "Hi, This is Second Page";
?>
That isn't how wp_link_pages(); works. It only displays your the_content in pages if your the_content contains this tag <!--nextpage-->
sudo code
<?php
echo apply_filter ("the_content" , "Hi, This is First Page<!--nextpage-->Hi, This is second Page");
wp_link_pages();
?>
You might want to check out this question as to how to retain the page data if you are creating your own page content and not using the standard content area.
Wordpress PHP - need to retain nextpage paginating tag in custom query
When already in a php block, you can execute a function without opening another php block. According to the documentation, wp_link_pages(); should also be called without echo.
wp_link_pages();
Documentation:
https://codex.wordpress.org/Function_Reference/wp_link_pages
This works just fine:
global $post;
$total = substr_count($post->post_content, '<!--nextpage-->') + 1;
I'm working on a page template and I'm having trouble working out how I can echo a string of php. Basically I have set up a custom field inside Wordpress for the page called Slider Code, and it asks the user to paste in the slider code given to them so that the appropriate slider is displayed on the page. The slider code given looks like this: [soliloquy id="82"]
The custom field plugin gives me this code to use in the template to echo out whatever is in that field, which looks like this: <?php echo CFS()->get('slider_code'); ?>
In WordPress there is a php function called do_shortcode which enables me to use that code, so it would look like this: <?php do_shortcode('[soliloquy id="82"]'); ?> and the slider would be displayed. So is there any way I can merge that <?php echo CFS()->get('slider_code'); ?> string with the do_Shortcode string? If not, does anyone have any alternative solutions to this issue? I can only think of having to make separate page templates for each individual slider.
EDIT: I have just discovered that the do_shortcode function does not actually work at all with the soliloquy shortcode. It does give me the option of using this code: if ( function_exists( 'soliloquy' ) ) { soliloquy( '82' ); }. However, when I paste this code into the custom field as is, the <?php echo CFS()->get('slider_code'); ?> tag just echos out that text. If I wrap the code in the custom field in <?php and ?> it just comments the code out inside my inspector. How do I get around this?
Check this out.
$slider = CFS()->get('slider_code');
call_user_func('do_shortcode', $slider);
I don't understand why you can't just run do_shortcode(CFS()->get('slider_code')).
Ok. Then try this one. Paste this code in the template file.
<?php
if ( function_exists( 'soliloquy' ) ) {
soliloquy( CFS()->get('slider_code') );
}
?>
And for slider_code input just enter '82'. If you are unable to see what is the template file you have to paste the above given code, then try to see WordPress template conventions.
I have to use some WordPress short-codes for a project - but I want them in very specific places, and not in the main content. I need the client to just put them in specific Custom fields. Using ACF, I built a text field specifically for this.
// normal in-code use
<?php echo do_shortcode('[example_shortcode]'); ?>
// ACF field
<?php the_field('my_main_shortcode'); ?>
// ACF field in the do_shorcode
<?php echo do_shortcode( the_field('my_main_shortcode') ); ?>
This renders out [example_shortcode] on the page instead of what I would expect.
I'm guessing this is a standard PHP thing - and I don't know how to escape or concatenate it properly. EDIT - below using get_fields gets me closer...
THEN... with a more complex short-code
<?php echo do_shortcode( '[tf_listview imagesize="medium1" exclude="calendar-link,list-view-title" dateformat="D n.j"]' ); ?>
It breaks somethings like the image by adding "" - which could totally be the way the short-code was written / but basically - I haven't found a solution to using a short-code in a custom field like this.
Bottom line,
I'd like to write: <?php shit_out('my_annoying_shortcode'); ?>
Or something as close to that as possible. AND have it actually work.
Can anyone give me some more direction?
Since you're using echo, you need to use get_field(), which returns the custom meta value:
<?php echo do_shortcode( get_field('my_main_shortcode') ); ?>
I'm using the Advanced Custom Fields plugin for WP which I really like, because it gives me the ability to add a whole bunch of custom meta boxes to individual pages within my site, and implementation within page templates is very straightforward. This is generally how I would use it with a template:
<?php if( get_field('FIELD-NAME-HERE') ): ?>
<h6><?php the_field('FIELD-NAME-HERE'); ?></h6>
<?php endif; ?>
In this case, I have a field for my Contact Form 7 Shortcode, and I've turned formatting off for this field, so it should return exactly what is entered (ie. [contact-form-7 title="Contact Form"] ).
I know that I can use the do_shortcode(); to pull this shortcode into my page template, but I want to be able to populate that do_shortcode(); with the_field(); from above.
I've tried this:
<?php if( get_field('form_shortcode') ): ?>
<?php echo do_shortcode("<?php the_field('form_shortcode'); ?>");?>
<?php endif; ?>
And this:
<?php
var formCode = the_field('form_shortcode');
echo do_shortcode(formCode);
?>
I'm still pretty new to PHP, but I feel like I'm not too far off. Any help would be greatly appreciated!
You're not far off. You need to use get_field() rather than the_field(): the_field() actually echoes out the content, so that's why it's not working as currently using it. It's the equivalent of trying to echo it twice.
Another thing to look out for is that you need to include the square brackets when using do_shortcode() but since your code already includes them you don't need to worry about it in this instance.
<?php if( get_field('form_shortcode') ): ?>
<?php echo do_shortcode( get_field('form_shortcode') );?>
<?php endif; ?>
i am trying to insert a post/page into one of my themes files and it wont display shortcodes or php
i have created a page called home in the wordpress admin-paned and inserted into my code the following:
<div id="home_page"> <!-- echos the content from the page "home" id:43 -->
<?php $home_id = 43;
$home_page = get_page( $home_id );
?>
<?php echo $home_page->post_content; ?>
</div> <!-- end #home_page -->
and non of the shortcodes that i have in the page work.
i installed a php in post or page and tried useing php and it doesnt work.
when i insert
echo do_shortcode('[youtube_sc url=http://www.youtube.com/watch?v=Db3XGpt6nNU]');
directly into the code it works.
does anyone know y this happens?
thank you.
I got an answer in wordpress.stackexchange.com
I Quote:
You need to apply the filter the_content e.g.:
<?php echo apply_filters('the_content',$home_page->post_content); ?>
Also, you don't need custom shortcodes for youtube, just put the URL in the content (but not a hyperlink), and it'll be swapped out for a youtube player at runtime. No plugins or extra code needed thanks to oembed.
Thank You Tom J Nowell
The $home_page->post_content value is the exact post content as stored in the database. Echoing this does not give any of your shortcodes a chance to run.
You should use a "WordPress Loop" to display the content, as this sets things up to allow you to use template tags such as the_title() and the_content() - this will call the processing functions for shortcodes and other functions like wpautop() that "massage" post content for output.
If you don't want to use a Loop, you could output the content using
echo do_shortcode($home_page->post_content);
This will run the post content through the shortcode processor, giving the shortcodes a chance to run.
For more on how WordPress "massages" post content, you can look here: http://codex.wordpress.org/How_WordPress_Processes_Post_Content