WordPress - Executing PHP in Theme Options Panel - php

I have created a theme options panel for a client however they would like the ability to enter PHP code in the textareas and have it executed on the front end.
However, when they enter the code, it does not display properly in the front end, please see the following two screenshots:
http://i.imgur.com/alOAD.png
http://i.imgur.com/pYhW0.png
It looks like the code is being stripped when displayed on the front end. Its displayed using this code:
<?php global $options;
foreach ($options as $value) {
if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_settings( $value['id'] ); }
}
?>
<?php echo $ag_footer_top; ?>
How can I get it to work properly? Is it possible?
If I enter the following into the theme options:
<?php wp_nav_menu( array( 'theme_location' => 'first','fallback_cb'=> ” ) ); ?>
It gets saved INTO the database as:
<?php wp_nav_menu( array( \'theme_location\' => \'first\',\'fallback_cb\'=> ” ) ); ?>
And its displayed on the front end as:
\'first\',\'fallback_cb\'=> ” ) ); ?>

First of all you must prevent WordPress from adding slashes to your content, using something like:
update_option('my_option',stripslashes($_POST['my_option']));
The other thing is that you want your code to be executed... well I do not know how to do this exactly, but a lot of Plugins provide this functionality, like
Linkable Title Html and Php Widget
You should download the Plugin and figure out how it works.
Just take the content of the field from the database now and try to parse it in some way.

Use stripslashes to unescape the string before evaluating/displaying the code.

You should replace ” with ''

you can use this functon of PHP
PHP: eval();
you can take the code in textarea save it to options as string and whenever you want to execute it, retrieve from options and pass it to eval.
Thanks
-Shak

I personally use Exec-PHP. It should be quite easy for your clients to use.

Related

Simple Shortcode (Wordpress) does not work

This is my "code":
function whoop_function(){
return "whoop whoop!";
}
add_shortcode('whoop', 'whoop_function' );
Now when I want to use it in a post, all I get is:
[whoop]
As you can see I am very new and unused to it so maybe the answer is really simple, maybe I've just miss a thing in advance.
I both checked defining the function in functions.php and also in content.php
For shortcode function echo is required.
Please check with below code
function whoop_function(){
$responseData = "whoop whoop!";
echo $responseData;
return true;
}
add_shortcode('whoop', 'whoop_function' );
put your code in themes/function.php files and remove from content.php as function is duplicated so function already defined PHP error occurred.
function whoop_function(){
return "whoop whoop!";
}
add_shortcode('whoop', 'whoop_function' );
and add your shortcode [whoop] in any page content section.
if u use do_shortcode('[whoop]'); then echo it like below.
<?php echo do_shortcode('[whoop]'); ?>
Your approach is correct. I think you are using it in a template. Just need to use it as mentioned below :
In files:
<?php echo do_shortcode('[whoop]'); ?>
In admin pages or posts:
[whoop]
Fetch the content like this:
$post_content = get_post(get_id_by_slug('short')); // assuming that you have already defined get_id_by_slug function as you are using it in your code and "short" is a slug for post you are fetching content
$content = $post_content->post_content;
echo do_shortcode( $content );
You are fetching content without considering the short code. Update your code as above.
Hope it will work for you.

Wordpress plug in php issues

I'd like to preface that I am not too fluent in php so i'm trying to understand and build php as i go.
I am attempting to add a custom Slidedeck slider to my website, http://cfsb.org/ . I currently have one in the header, but would like to change it on a per page basis.
I currently have:
function slidedecktwo_header_home() {
if (is_home()) {
echo do_shortcode('[SlideDeck2 id=xxx]');
}
}
add_action('genesis_header', 'slidedecktwo_header_home');
But when i try to add something like
else if (is_page(PAGE_ID = x)) {
echo do_shortcode('[SlideDeck2 id=xxx');
}
My webpage crashes and I have to delete the code before my website returns. Anyone see some obvious problems?
Thanks in advance.
Not sure if what you posted is the actual PHP you tried, but there are definite syntax errors.
Your code should look more like this (I added a random ID passed as an integer to the is_page() function):
function slidedecktwo_header_home() {
if ( is_home() ) {
echo do_shortcode('[SlideDeck2 id=xxx]');
} elseif ( is_page(42) ) {
echo do_shortcode('[SlideDeck2 id=xxx]');
}
}
add_action('genesis_header', 'slidedecktwo_header_home');
In your snippet, you had is_page( PAGE_ID = x ) and your shortcode snippet wasn't closed with a closing bracket ]

Using Meta Box Plugin to assign URL to a variable

I'm a little lost here, hoping that someone can help. I'm using the Meta Box plugin for WordPress, and I'm trying to create a process for the user to select an option from a predefined list, and then assign a URL to that option as a link. Im trying to define the URL in a variable, and then call it in a function, but I'm still a little green on PHP syntax. this is my code now:
<?php
$article_url= rwmb_meta('orion_2016_article_url', 'type=URL');
if (rwmb_meta('orion_2016_article_source') != '') {
echo '<a href= ("$article_url") target=blank>';
echo rwmb_meta('orion_2016_article_source');
echo '</a>';} ?> on <?php the_date(); ?>
Since the options are already predefined, it seems like assigning a random URL to one of the options should be pretty simple. Hopefully this makes sense!
You need to to place variables you wish to echo inside double quotes or simply concatenate strings using . as in my example. Note that I didn't check the plugin's specific syntax, only general PHP syntax.
<?php
$article_url= rwmb_meta( 'orion_2016_article_url', 'type=URL' );
if (rwmb_meta('orion_2016_article_source') != '') {
echo '' . rwmb_meta( 'orion_2016_article_source' ); . '';
} ?> on <?php the_date(); ?>

Using shortcode breaks RSS feed

I had successfully created shortcode on my wordpress website to use it in the posts. But I later realised that it is breaking my rss-feed.
Here is the shortcodes.php
<?php
function disclaimer() {
$disc = " <p style='font-style:italic;'> <strong>Disclaimer</strong> :</p>";
return $disc;
}
add_shortcode('disclaimer', 'disclaimer');
?>
Here is how I am including it on the functions.php
<?php
include 'shortcodes.php';
So now, I am getting this error when I access my rss URL on my browser
So when I removed this include 'shortcodes.php'; my rss feed started working. But then I also want my shortcode to work. Am I doing anything wrong here? Any help would be highly appreciated.
You can change your function so that the disclaimer is not added on feed page
function disclaimer() {
if ( ! is_feed() ) {
$disc = " <p style='font-style:italic;'> <strong>Disclaimer</strong> :</p>";
return $disc;
}
}
Edit:
Then it is not a problem with the code, but some problem with the file.
Make sure there is not extra space after the closing ?> php tag. Or better, try to remove it completely.

Unexpected code printed instead of executed

I'm using Advanced Custom Fields to have a field that the user can input YouTube embed code. In the post loop I have this code:
$embed_code = get_field( 'video_embed_code' );
if ( $embed_code ) {
echo do_shortcode( '[row]<div class="video-embed-code">' . $embed_code . '</div>[/row]' );
} else
(the else statement gets an image and it is working properly no errors) The output is unexpectedly doing this in the browser though:
Why is it printing that iframe code not executing it?
The correct answer as #reikyoushin mentioned above is to change the "Format" settings to "Convert HTML into tags". I didn't realize that was a setting... Thanks for the answer!

Categories