Okay, so you can add shortcodes to WP template files like:
<?php echo do_shortcode('[my_awesome_shortcode]'); ?>
but what if the shortcode is intended to wrap around content like this:
[my_awesome_shortcode]
Hey, this is the content within the awesome shortcode.
[/my_awesome_shortcode]
I'm a bit unsure how to put that into a template file.
The solution that worked for me was to combine shortcodes into a single string, so
<?php echo do_shortcode('[my_awesome_shortcode]<h1>Hello world</h1>[/my_awesome_shortcode]'); ?>
will work!
If you have a long chain of shortcode commands you want to execute, create a separate string for them like this
$shortcodes = '[row]';
$shortcodes .= '[column width="2/3"]';
$shortcodes .= 'Content';
$shortcodes .= '[/column]';
$shortcodes .= '[column width="1/3"]';
$shortcodes .= 'More Content';
$shortcodes .= '[/column]';
$shortcodes .= '[/row]';
Then execute the entire thing like this
<?php echo do_shortcode($shortcodes); ?>
According to http://codex.wordpress.org/Shortcode_API#Enclosing_vs_self-closing_shortcodes
adding $content = null to the shortcut function should do the trick:
function my_awesome_shortcode_func( $atts, $content = null ) {
return '<awesomeness>' . $content . '</awesomeness>';
}
add_shortcode( 'my_awesome_shortcode', 'my_awesome_shortcode_func' );
so that:
[my_awesome_shortcode]
Hey, this is the content within the awesome shortcode.
[/my_awesome_shortcode]
would result in:
<awesomeness>Hey, this is the content within the awesome shortcode.</awesomeness>
Related
I have the following code that takes the content from the Wordpress post and injects an image between each count of a paragraph - this works well.
It does not take into account heading or image tags etc. How can I modify this so it does take these other tags into the count?
<?php
$content = apply_filters( 'the_content', get_the_content() );
$content_table = explode("<p>", $content);
$content_table[3] .= $spot1;
$content_table[6] .= $spot2;
$content_table[9] .= $spot3;
$content_table[12] .= $spot4;
$content_table[15] .= $spot5;
$content2 = implode($content_table, "<p>");
echo $content2;
?>
I am trying to add a block to all my posts, i am using the_content Hook to add the shortcode, the code below adds the shortcode in the end of the content, is there something i can do to put the shortcode for example after 25% of the content?
add_filter( 'the_content', 'my_shortchode_in_single_page' );
function my_shortchode_in_single_page($content){
if(is_single())
return $content . do_shortcode('SHORTCODE HERE');
return $content;
}
Actually we can't found 25% of content but we can put it after a certain number of p tag. you can 0 to which number of p tag in content.
add_filter('the_content', 'ak_the_content_filter', 10, 1);
function ak_the_content_filter($content)
{
$parags = explode('</p>', $content);
$parags[0] .= '<br>'.do_shortcode('[SHORTCODE HERE]');// add whatever you want after first paragraph
$content_new = '';
foreach ($parags as $parag) {
$content_new .= $parag;
}
return $content_new;
}
I am creating a private wordpress shortcode for an accordion, by using a simple plugin, created by myself.
The problem: Wordpress adds breaks and paragraphs to my HTML outcome, which leads to a broken design.
I can remove autobreaks, but then it also does not work for the real content, which has paragraphs. But I dont want to use p-tags in the backend.
How can I stop wordpress from doing autobreaks to my HTML, but still doing it for my wrapped content?
The solution has to work within a plugin - without affecting other plugins/functions.
I can remove any spacing between my shortcodes, so wordpress does not add any breaks. But thats very ugly to read.
This way it works:
[os_accordion][os_spoiler title="Title"]Content with some
breaks.[/os_spoiler][/os_accordion]
This way it does not:
[os_accordion]
[os_spoiler title="Title"]
Content with some
breaks.
[/os_spoiler]
[/os_accordion]
I tried to use a function for removing breaks, but it does not work.
//Helper to remove autops
function cleanup_shortcode_fix($content) {
$array = array('<p>[' => '[', ']</p>' => ']', ']<br />' => ']', ']<br>' => ']');
$content = strtr($content, $array);
return $content;
}
//Outer accordion wrapper
function function_accordion($atts, $content = null){
$html = '<div class="accordion">'.do_shortcode($content).'</div>';
return cleanup_shortcode_fix($html);
}
add_shortcode('os_accordion', 'function_accordion');
//Inner wraps
function function_spoiler($atts, $content = null){
//set default attributes and values
$values = shortcode_atts( array(
'title' => '',
), $atts );
//Output buffer
ob_start();
?>
<div class="toggle"><?php echo esc_attr($values['title']); ?></div>
<div class="content"><?php echo $content; ?></div>
<?php
return ob_get_clean(); //Close buffer and return data
}
add_shortcode('os_spoiler', 'function_spoiler');
Any ideas how I can make the last version work? I'm stuck. :/
This should do it. It's probably just putting p tags after everything.
remove_filter('the_content', 'wpautop');
I am using get_posts to retrieve posts information from database. It returns "Post title", "thumbnail", "post category", and "post excerpt". Everything is working fine but the problem is I am unable to show post excerpt.
Here is my code:
function widget ($args,$instance) {
extract($args);
$title = $instance['title'];
$catid = $instance['catid'];
$numberposts = $instance['numberposts'];
$date = $instance['date'];
$rss = $instance['rss'];
// retrieve posts information from database
global $wpdb;
$posts = get_posts('post_type=post&numberposts='.$numberposts.'&category='.$catid);
$out = '<ul>';
if ($posts) {
foreach($posts as $post) {
setup_postdata($post);
$out .= '<li>'.get_the_post_thumbnail($post->ID,'medium').'</li>';
$out .= '<li>'.$post->post_title.'</li>';
$out .= '<li>'.$post->post_excerpt.'</li>';
if ($date) $out .= '<li>'.date('d/m/Y', strtotime($post->post_date_gmt)).'</li>';
}
}
if ($rss) $out .= '<li>Category RSS</li>';
$out .= '</ul>';
//print the widget for the sidebar
echo $before_widget;
echo $before_title.$title.$after_title;
echo $out;
echo $after_widget;
}
}
$post->post_excerpt does not get work the way you think it does. Most people think this is the same as the template tag the_excerpt(), and it is not
the_excerpt() is generated by truncating get_the_content(). $post->post_excerpt is not generated at all as this is user defined. This excerpt is the excerpt text manually added by the user in the post edit screen in the excerpt meta box. (This meta box is hidden by default, but can be enabled in the "Screen Options" tab on the top part of the screen). If the user did not specify a manual excerpt, $post->post_excerpt will return nothing, that is why you see this behavior
You have already set up your postdata, so you can just simply use the template tags directly, so in place of $post->post_excerpt, you can use the_excerpt()
EDIT
Thanks to the comment below, I did not take into account that the excerpt should not be echo'd straight away. In this case, you would make use of get_the_excerpt() which does not echo the text, but simply retrieves it.
I'm trying to make a WordPress plugin. Well, actually it's done and fully working, except one thing.
I have added a shortcode for the plugin. But no matter where in the content I call this shortcode, the contents it gets are always on top of the post, instead of where I placed the tag.
The code that outputs something:
public static function showIncomingSearches(){
global $id;
$arSearches = self::getArObj(array('wp_post_id' => $id));
ob_start();
if(!empty($arSearches)){
$str = '<ul>' . PHP_EOL;
foreach($arSearches as $oSearch){
$str .= '<li>'.htmlspecialchars($oSearch->searchterm).'</li>' . PHP_EOL;
}
$str .= '</ul>';
if(!empty($arSearches))
echo $str;
} else {
echo ' ';
}
return ob_get_clean();
}
And the shortcode functionality:
add_shortcode('show_incoming_searches', 'checkReferrer');
function checkReferrer(){
incomingSearches::checkReferrer();
echo incomingSearches::showIncomingSearches();
}
What I want to know though, is why it is always on top of the content?
Your shortcode code needs to return the content, not echo it.
function checkReferrer(){
incomingSearches::checkReferrer();
return incomingSearches::showIncomingSearches();
}