Unexpected code printed instead of executed - php

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!

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 ]

Wordpress not showing up when using PHP via plugin?

I have installed a plugin (use PHP in posts) for Wordpress, and am having some issues.
Here is my code (though I don't think its the issue)
$usertoget = $_GET['player'];
$partone = "http://website.co.uk/path/to/php/file.php";
if(strlen($usertoget) != 0) {
$parttwo = "&player=";
$partthree = $usertoget;
$finalurl = $partone . $parttwo . $partthree;
print '<iframe src="$finalurl"></iframe>';
}
else {
print "<iframe src='/path/file.php'>";
}
The iframe gets printed perfectly and does what the target file should, but as you can see from this picture, messes up the formatting:
No sidebar;
No footer;
No floating header.
I have noticed that the page source finishes the iframe and does nothing else except close (</body></html>). There is a lot of stuff after it that should appear but doesn't.
Another issue is, using GET requests (/page/hello?player=9 and /page/hello/?player=9) returns a 404. Any ways to resolve this issue?
Does anybody know how I can fix this? I can post any more code if required :)
Any help would be appreciated :D Thanks!
If PHP suddenly stops sending the expected output, it usually means there's a PHP error. Is there anything in your logs? Have you tried turning WP_DEBUG on?
I'm surprised you say the iframe gets printed perfectly. This line
print '<iframe src="$finalurl"></iframe>';
looks wrong to me (though it won't cause PHP to error). If you have single quotes around a string, variables won't be evaluated. See this question for a little more detail.
You have mistake in concatenation of $finalurl .you can check you html its print the $finalurl variable as it is.Its didn't outputted its values .I had check it.So you just have to concatenate the $finalurl variable correctly .Here is your code which i had made corrected.
$usertoget = $_GET['player'];
$partone = "http://website.co.uk/path/to/php/file.php";
if(strlen($usertoget) != 0) {
$parttwo = "&player=";
$partthree = $usertoget;
$finalurl = $partone . $parttwo . $partthree;
print '<iframe src='.$finalurl.'></iframe>';
}
else {
print "<iframe src='/path/file.php'>";
}
if you have any query please comment bellow.

WordPress - Executing PHP in Theme Options Panel

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.

Changing a php "echoed" div attribute with php

I'm using PHP to echo a content stored on my database. The content is a DIV carrying any type of data.
The problem is that I don't know the ID and I have some problems with these DIVs if I try to display them more than once.
So, the idea is to modify the DIV id each time I'd like to display them.
Something like this:
<?php modify_div_id($data,"id-456"); ?>
How would I go about doing this?
This is ugly, but i think it works.
function modify_div_id( $data, $new_id ) {
return preg_replace( '/(<div[^>]+?id=)("|\')(.*?)("|\')/i', '$1$2' . $new_id . '$4', $data );
}
The best way to go is to use an XML parser to change the attribute.
Edit: the function assumes the div to already have an id attribute.
Edit #2
It seems to be working!
jwandborg#sophie:~$ cat | php -r "eval( file_get_contents('php://stdin') );"
function modify_div_id( $data, $new_id ) {
return preg_replace( '/(<div[^>]+?id=)("|\')(.*?)("|\')/i', '$1$2' . $new_id . '$4', $data );
}
echo modify_div_id('<div pajas="pajas" id="fisk">Innehåll</div>', 'nytt_id');
# Result: <div pajas="pajas" id="nytt_id">Innehåll</div>
Your question is worded kind of vague.
First off, the id attribute for HTML elements must be unique, so echoing a unique id is appropriate (you can also use a class).
If you've queried your database and received back, say, an array containing an id and the content, you can use the function as follows:
function modify_div_id($data, $id) {
echo "<div id='$id'>$data</div>";
}
Just an example.

Categories