Wordpress: custom content shows before $content - php

I'd like to show some custom content after the_content. However, I can't make it work. the custom content always shows before the_content. Here's my code:
function after_content($content){
function custom_content() {
include('page.php');
...
}
$content = $content . custom_content();
return $content;
}
add_filter('the_content', 'after_content');

Related

Wordpress shortcodes bug in custom theme

I've created a theme from scratch and I have issues creating shortcodes. I have the following code:
functions.php
function caption_shortcode( $atts, $content = null ) {
return '<span class="caption">' . $content . '</span>';
}
add_shortcode( 'caption', 'caption_shortcode' );
in the WP Admin page editor:
[caption]My Caption[/caption]
on the page template page:
echo do_shortcode('[caption]');
The shortcode seems to be somehow working as it returns the HTML but not the $content.
My problem is that I can't seem to get my hand on the $content and display it using the shortcode. Any idea why this is happening?
P.S. I don't want to use the_content() function to display all the content, I want to use the shortcodes to divide the content the user adds in several pop-ups and child sections of the page.
Thanks!
Make sure you user shotcode same page
// [baztag]content[/baztag]
function baztag_func( $atts, $content = '' ) {
return $content;
}
add_shortcode( 'baztag', 'baztag_func' );
echo do_shortcode('[baztag]');

WordPress shortcode in text widget not passing content

I have an enclosing widget which works fine in post/page but when in a text widget it's not passing the content between the shortcode tags.
The shortcode looks like:
[wpbutton]wpbutton[/wpbutton]
and the code is:
add_shortcode('wpbutton', array($this, 'shortcode'));
add_filter('widget_text', array($this, 'shortcode'));
function shortcode($args, $content = null) {
extract(shortcode_atts(array(
'action' => '',
'classes' => 'wpbutton',
), $args));
echo wpbutton($action, $content, $classes);
// Added for testing - echos in sidebar!
if (!$content) {
echo 'no content';
}
}
function wpbutton($action, $content, $classes) {
// Do stuff
}
In post/page content it echos whatever is between the tags but in a text widget it echos "no content"
Anyone know how to fix?
I think you should change your filter function to this:
add_filter('widget_text', array($this, 'do_shortcode'));
instead of
add_filter('widget_text', array($this, 'shortcode'));
It passes the content of the text widget to the do_shortcode() function of Wordpress, instead of your function. The Wordpress function will handle the shortcodes by itself and call your shortcode function automatically.
As seen here and here.

Adding <!--nextpage--> dynamically to WordPress post

Basically, I'm trying to add the tag <!--nextpage--> to generate the pagination of the post dynamically using some shortcode in a plugin,
I've tried to use the following code to do such functionality.
public function __construct() {
add_shortcode('CONTINUED', array(&$this, 'continued_handle'));
}
public function continued_handle() {
global $post;
add_filter('the_content', array(&$this, 'your_post_split'));
return $this->your_post_split($post);
}
public function your_post_split($content) {
$output = '<div>In page 1</div>';
$output .= '<!--nextpage-->';
$output .= '<div>In page 2</div>';
return $output;
}
When I use the shortcode [CONTINUED] on a page, I want it to echo <div>In page 1</div> then process the <!--nextpage--> like it normally would in WordPress and start the pagination.
What it's actually doing is returning this in the post
<div>In page 1</div><!--nextpage--><div>In page 2</div>
It's not actually doing the functionality of <!--nextpage--> in which I want it to

Wordpress if is_page function not working

I'm trying to add a function to my Wordpress theme that will add content before and after each post, but not on any pages. This does not seem to work, but not sure why.
if(!is_page()) {
function custom_content($content) {
$content = 'before post content' . $content . 'after post content';
return $content;
}
add_filter('the_content','custom_content');
}
EDIT: Solution I ended up with that does the trick for me, using is_single to only include on single posts. If you want to include on single pages use is_singular
function custom_content($content) {
if (is_single()) {
$content = 'before post content' . $content . 'after post content';
}
return $content;
}
add_filter ('the_content', 'custom_content', 0);
You might be using it inside the loop. is_page() works only outside the loop.
https://codex.wordpress.org/Function_Reference/is_page

WordPress plugin content after the_content using add_filter()

I'm trying to add my_custom_function after the_content in WordPress using
add_filter ('the_content', 'my_custom_function');
add_action ('the_content', 'my_custom_function'); // I tried both
My my_custom_function function is like this
function my_custom_function($content) {
if(is_single()) {
$content .= "this is custom function content";
$content .= my_another_custom_function();
$content .= "this is custom function content";
}
return $content;
}
My problem is this code is appending my_another_custom_function() before the_content but I want it after the content.
The other 2 lines given in the code $content .= "this is custom function content"; is for testing and these are appearing after the_content
Can someone please give me any idea about how to fix it and what I'm doing wrong...
The most possible reason is that my_another_custom_function() echoes the output instead of just returning it.

Categories