WordPress: add shortcode into a concatenate variable - php

I'm trying to make this work but it's not working. What's wrong with this piece of code?
add_filter( 'the_content', 'addslider_hook' );
function addslider_hook ( $content ) {
if ( is_page('outsourced') ) {
$slide = '[rev_slider outsourced]';
$hook_slide = do_shortcode( $slide );
$content .= "<div class='page-slider'>";
$content .= $hook_slide;
$content .= "</div>";
}
return $content;
}

You have a extra } at the bottom of the code.You can try this
if ( is_page('outsourced') ) {
$content .= "<div class='page-slider'>";
$content .= do_shortcode('[rev_slider outsourced]');
$content .= "</div>";
echo $content;
}

Related

apply_filters not echoing list items in Unorderd List

I am using wordpress apply_filters hook for attaching an action hook.My code is to echo lists if some links in a ul tag.(if woocommerce active).
My code goes here
$loggedin_item = '<div>';
$loggedin_item .= $user_dp; //it works & its none of this questions business
$loggedin_item .= '<ul>';
apply_filters( 'after_user_loggedin_menu', $is_wc_active, $is_logged_in ); //its not working
$loggedin_item .= '</ul>';
$loggedin_item .= '</div>';
and my attachment function is here.If i use echo it echos outside ul and if i use return it does not echo anything
function wc_action_header( $is_wc_active, $is_logged_in ){
$wc_li = '';
if( $is_wc_active){
$my_account_url = get_permalink( get_option('woocommerce_myaccount_page_id') );
$wc_account_items = wc_get_account_menu_items();
foreach ($wc_account_items as $key => $value) {
$wc_li .= '<li>'. esc_attr($value) .'</li>';
}
}
echo $wc_li;
}
and adding hooks
add_filter('after_user_loggedin_menu', array($this, 'wc_action_header' ), 10, 2);
Since you're storing your HTML in $loggedin_item var, you need to add wc_action_header output to this var.
$loggedin_item = '<div>';
$loggedin_item .= $user_dp; //it works & its none of this questions business
$loggedin_item .= '<ul>';
$output = '';
$output = apply_filters( 'after_user_loggedin_menu', $is_wc_active, $is_logged_in );
$loggedin_item.=$output;
$loggedin_item .= '</ul>';
$loggedin_item .= '</div>';
And change echo $wc_li; to return $wc_li; in your wc_action_header function.

Why do my [shortcodes] show as plain text from the WP editor?

I'm trying to use shortcodes to execute tag-specific content loops on different pages of my website. I know my shortcode function is working properly, because when I hardcode do_shortcode into my page template, it shows up perfectly.
But when I try to add a [shortcode] directly into the Wordpress editor instead, it shows as plain text. Any ideas how I can fix this?
You can see what I'm talking about here - the [showtag tag="seefour"] you see as plain text is written directly into the Wordpress text editor. It's not working correctly. Just below it, you'll see the <?php echo do_shortcode("[showtag tag='seefour']"); ?> properly executing my content loop from the page template.
Any ideas how I can fix this? Hardcoding do_shortcode is not sustainable for me. The site currently only has two active plugins, but this problem persists after deactivating them, so I'm at a loss.
For good measure, this is the content loop I'm trying to execute:
function showtag_shortcode( $atts ) {
$atts = shortcode_atts( array(
'tag' => '', // Default value.
), $atts );
$posts = get_posts( 'tag=' . $atts['tag'] );
if ( $posts ) {
$output .= '<div class="jd-container">';
$output .= '<section class="jd-grid jd-pad1">';
foreach ( $posts as $post ) {
setup_postdata( $post );
$output .= '<div class="jd-box">';
$output .= '<a href="' . get_the_permalink( $post ) . '">';
$output .= get_the_post_thumbnail( $post );
$output .= '<div class="jd-overlay"></div>';
$output .= '<div class="jd-overlay-text">';
$output .= get_the_title( $post );
$output .= '</div>';
$output .= '</a>';
$output .= '</div>';
}
$output .= '</section>';
$output .= '</div>';
} else {
$output = 'no data';
}
wp_reset_postdata();
return $output;
}
add_shortcode( 'showtag', 'showtag_shortcode' );
And here's my page.php template code:
<?php get_header(); ?>
<section class="jd-backdrop">
<div class="jd-trans-row jd-container">
<h2 class=""><?php the_title(); ?></h2>
<br>
<p class=""><?= get_post_field('post_content', $post->ID) ?></p>
</div>
<?php echo do_shortcode("[showtag tag='seefour']"); ?>
</section>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
None of the solutions I've found so far have worked, so I'm open to suggestions...
It seems that your theme does not execute do_shortcode() with the content of the post.
Try to add the following to functions.php
function the_content_filter( $content) {
return do_shortcode( $content);
}
add_filter( 'the_content', 'the_content_filter', 1000);
UPDATE
From the code of your theme we can see that you use get_post_field to output content of the post. Unlike to the_content(), this function does not invoke any filters. That is why code above does not work in your case.
You have to use get_post_field() in the following manner:
<?php echo do_shortcode( get_post_field( 'post_content', $post->ID ) ); ?>
P. S. You should also avoid to use <= as it doesn't work on most hostings and is discouraged.
See this code from a plugin that I am worked on, this was ages ago but you can see that the "do_shortcode()" function is included, try add it to your $output.
function rir_row( $params, $content = null ) {
extract( shortcode_atts( array(
'class' => 'rir-row'
), $params ) );
$content = preg_replace( '/<br class="nc".\/>/', '', $content );
$result = '<div class="' . $class . '">';
$result .= do_shortcode( $content );
$result .= '</div>';
return force_balance_tags( $result );
}
add_shortcode('rir_row', 'rir_row');
function rir_item( $params, $content=null ) {
extract( shortcode_atts( array(
'class' => 'col-sm-1'
), $params ) );
$result = '<div class="' . $class . '">';
$result .= do_shortcode( $content );
$result .= '</div>';
return force_balance_tags( $result );
}
add_shortcode( 'rir_item', 'rir_item' );

Return different code if variable is a string or not

I want to return a small block of code if the variable is a string, but if it is empty just return text "no image".
function link_img_preview( $atts, $content = null ) {
if ( is_string( $content ) ) {
$content = do_shortcode( str_replace( '###SPACE###', '', $content ) );
}
if ( is_string( $content ) == false ) {
$content = 'no image';
}
require_once('OpenGraph.php');
$graph = OpenGraph::fetch($content);
$return = '<img class="link-image" src=';
$return .= $graph->image;
$return .= '>';
return $return;
}
No need to overcomplicate things. The default value $content = null will evaluate as false, so if you don't provide the second argument you will get 'no image'. If you pass an empty string it will evaluate as false, so you will get 'no image'. If you pass something that is not a string to this function that appears to be expecting a string, it will throw an error, which seems to be a reasonable response.
if ($content) {
$content = do_shortcode( str_replace( '###SPACE###', '', $content ) );
} else {
$content = 'no image';
}
use empty()
if(empty($yourstring))
{
//your code
}
Code:
function link_img_preview( $atts, $content = null ) {
if (!empty($content)) {
$content = do_shortcode( str_replace( '###SPACE###', '', $content ) );
}else{
$content = 'no image';
}
require_once('OpenGraph.php');
$graph = OpenGraph::fetch($content);
$return = '<img class="link-image" src=';
$return .= $graph->image;
$return .= '>';
return $return;
}
Check if $content is a string as you do it already, but check also if the string is empty. Something like $a = ""; is a string too.
The code that does what you want is the following
function link_img_preview( $atts, $content = null ) {
if ( !is_string( $content ) || empty ( $content) ) {
$content = 'no image';
} else {
$content = do_shortcode( str_replace( '###SPACE###', '', $content ) );
}
require_once('OpenGraph.php');
$graph = OpenGraph::fetch($content);
$return = '<img class="link-image" src=';
$return .= $graph->image;
$return .= '>';
return $return;
}
Also, just a little cleaner, adding in double quotes around src val
$return = '<img class="link-image" src="';
$return .= $graph->image;
$return .= '" >';
(see the " in the last line)
Ok so after looking at your examples I realized that I had it all wrong in the first place. Here is what ended up working out for me. Thanks so much for the help!
function link_img_preview( $atts, $content = null ) {
require_once('OpenGraph.php');
if ( is_string( $content ) ) {
$content = do_shortcode( str_replace( '###SPACE###', '', $content ) );
}
$graph = OpenGraph::fetch($content);
if (!empty($graph->image)) {
$return = '<img class="link-image" src=';
$return .= $graph->image;
$return .= '>';
return $return;}
else{
return 'no image';
}
}

Select certain tags to display instead of all tags in WordPress

In WordPress I am display all the tags by using this function
<?php
$tags = get_tags();
$html = '<div class="post_tags">';
foreach ( $tags as $tag ) {
$tag_link = get_tag_link( $tag->term_id );
$html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
$html .= "{$tag->name}</a>";
}
$html .= '</div>';
echo $html;
?>
This then prints out all the tags.
Instead of display all tags is there a way I can say which tags I wish for it to display the slug and link to?
I have also tried this
<?php
$tags = query_posts('tag=html');
$html = '<div class="post_tags">';
foreach ( $tags as $tag ) {
$tag_link = get_tag_link( $tag->term_id );
$html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
$html .= "{$tag->name}</a>";
}
$html .= '</div>';
echo $html;
?>
I am sure the answer is on this page but I can not come up with the solution http://codex.wordpress.org/Function_Reference/get_the_tags
It seems like this solution might not be much more maintainable than just linking them manually, but you could do something like...
<?php
$tags = query_posts('tag=html');
$html = '<div class="post_tags">';
$wanted_tags = array ( "aSlugThatIWant", "AnotherSlugThatIWant" );
foreach ( $tags as $tag )
{
if (in_array($tag->slug, $wanted_tags))
{
$tag_link = get_tag_link( $tag->term_id );
$html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
$html .= "{$tag->name}</a>";
}
}
$html .= '</div>';
echo $html;
?>
Edit: Just fixed a mistake.

How can I add Wordpress plugin hook to index.php?

I'm using a plugin called Category Thumbnail list. I normally would place the hook [categorythumbnaillist 100] on my Wordpress pages. How can I add this to an actual php page?
http://wordpress.org/extend/plugins/categoy-thumbnail-list/
Plugin code:
$categoryThumbnailList_Order = stripslashes( get_option( 'category-thumbnail-list_order' ) );
if ($categoryThumbnailList_Order == '') {
$categoryThumbnailList_Order = 'date';
}
$categoryThumbnailList_OrderType = stripslashes( get_option( 'category-thumbnail-list_ordertype' ) );
if ($categoryThumbnailList_OrderType == '') {
$categoryThumbnailList_OrderType = 'DESC';
}
$categoryThumbnailList_Path = get_option('siteurl')."/wp-content/plugins/categoy-thumbnail-list/";
define("categoryThumbnailList_REGEXP", "/\[categorythumbnaillist ([[:print:]]+)\]/");
define("categoryThumbnailList_TARGET", "###CATTHMBLST###");
function categoryThumbnailList_callback($listCatId) {
global $post;
global $categoryThumbnailList_Order;
global $categoryThumbnailList_OrderType;
$tmp_post = $post;
$myposts = get_posts('numberposts=-1&&category='.$listCatId[1].'&&orderby='.$categoryThumbnailList_OrderType.'&&order='.$categoryThumbnailList_Order);
$output = '<div class="categoryThumbnailList">';
foreach($myposts as $post) :
setup_postdata($post);
if ( has_post_thumbnail() ) {
$link = get_permalink($post->ID);
$thmb = get_the_post_thumbnail($post->ID,'thumbnail');
$title = get_the_title();
$output .= '<div class="categoryThumbnailList_item">';
$output .= '' .$thmb . '<br/>';
$output .= '' .$title . '';
$output .= '</div>';
}
endforeach;
/*
$output .= '</div>';
$output .= '<div class="categoryThumbnailList_clearer"></div>';
return ($output);
$output = '';
$post = $tmp_post;
setup_postdata($post);
*/
$output .= '</div>';
$output .= '<div class="categoryThumbnailList_clearer"></div>';
$post = $tmp_post;
wp_reset_postdata();
return ($output);
$output = '';
}
function categoryThumbnailList($content) {
return (preg_replace_callback(categoryThumbnailList_REGEXP, 'categoryThumbnailList_callback', $content));
}
function categoryThumbnailList_css() {
global $categoryThumbnailList_Path;
echo "
<style type=\"text/css\">
#import url(\"".$categoryThumbnailList_Path."categoy-thumbnail-list.css\");
</style>
";
}
add_action('wp_head', 'categoryThumbnailList_css');
add_filter('the_content', 'categoryThumbnailList',1);
?>
i think you want to add short-code in template file. place this line in your index.php
<?php echo do_shortcode( '[categorythumbnaillist 100]' ); ?>

Categories