Using shortcode breaks RSS feed - php

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.

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.

Using html code in php function or no?

i would to know what is good practice for writing code to put all HTML code inside PHP function and in my front index.php file just call function to show code.
class.php:
public function test() {
$sql='select id,title from test ';
$nem=$this->db->prepare($sql);
$nem->execute();
$nem->bind_result($id,$title);
echo '<ul class="centreList">';
while($nem->fetch())
{
echo '<li>'.$id.'<a href="'.$title.'" >Download</a></li>';
}
echo '</ul>';
}
index.php:
<?php $connection->test(); ?>
This work fine, but I would like to know is this proper way or is not a good practice to use html code inside PHP functions?
It's ok to build HTML within PHP, but I would not echo to the screen directly from within the function. Instead, return the built HTML string.
$html = '<ul class="centreList">';
while($nem->fetch())
{
$html .= '<li>'.$id.'<a href="'.$title.'" >Download</a></li>';
}
$html .='</ul>';
return $html
The function should not be responsible for pushing content to the browser because it really limits what you can do with your code. What if you wanted to further process the HTML? What if you run into a condition later in the code and decided to abort? What if you wanted to set some response headers later? Some content would already be gone so none of these things would be possible without clever workarounds.
In general you want to separate your responsibilities: I would even break things down further:
one piece of code is in charge of retrieving info from the DB and returning
Another piece is in charge of building the HTML string
A third piece is in charge of displaying the HTML (probably your index.php)
New index.php
<?= $connection->test(); ?>
Do not use echo to print the html directly, wrap the html within while loop surrounded by php tags
public function test() {
$sql='select id,title from test ';
$nem=$this->db->prepare($sql);
$nem->execute();
$nem->bind_result($id,$title);
return $nem;
}
<ul class="centreList">
<?php $res = test()->fetch();
while( $res->fetch() ) { ?>
<li> <?php echo $id ?> Download </li>;
<?php } ?>
</ul>

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 ]

Add paragraph based on tag (Wordpress)

I want to append a disclaimer paragraph to my single posts based on a tag, specifically my "affiliate" tag. I have tried a few codes I found online that show some text but cause my excerpt to be blank. I don't have as much PHP knowledge as I'd like, if someone could point me in the right direction to writing a function for this.
Wordpress has a function for this: https://codex.wordpress.org/Function_Reference/has_tag
This should work:
<?php if ( has_tag('affiliate') ) { ?>
<p>Disclaimer paragraph</p>
<?php } ?>
I got it to work with this code, it's appearing after my social share buttons.. but oh well.
`function affiliate_add_to_content( $content ) {
if( is_single() ) {
if (has_tag('affiliate')) {
$content .= 'The disclaimer here';
}
}
return $content;
}
add_filter('the_content', 'affiliate_add_to_content');`

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.

Categories