Wordpress if is_page function not working - php

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

Related

Yootheme Pro - Wordpress: Get content of page before it is displayed

I'm writing a simple plugin for wordpress that changes a single word on a page or a post to make it bold.
For example: vlbs -> vlbs
It works fine for normal Wordpress pages and posts with this code:
defined('ABSPATH') or die('You can\'t enter this site');
class VLBS {
function __construct() {
}
function activate() {
flush_rewrite_rules();
}
function deactivate() {
flush_rewrite_rules();
}
function unstinstall() {
}
function new_content($content) {
return $content = str_replace('vlbs','<strong style="color:#00a500">vlbs</strong>', $content);
}
}
if(class_exists('VLBS')){
$VLBS = new VLBS();
}
add_filter('the_content', array($VLBS, 'new_content'));
//activation
register_activation_hook(__FILE__, array($VLBS, 'activate'));
//deactivation
register_deactivation_hook(__FILE__, array($VLBS, 'deactivate'));
However, it does not work on a page built with Yootheme Pro Pagebuilder. Whatever is done within the function new_content() is processed after the content has already been loaded. Thus, I cannot manipulate it before it is displayed to the user.
So the question would be: How can I get the content of a page before it is displayed? Is there an equivalent to Wordpress' 'the_content'?
Any help is really appreciated! Thank you very much in advance.
Best regards
Fabian
Yootheme: 1.22.5
Wordpress: 5.2.4
PHP: 7.3
Browser: Tested on Chrome, Firefox, Edge, Internet Explorer
In your code, do you are sure it's the good usage of add_filter content ?
In the doc, the 2nd parameter is string, not array:
add_filter( 'the_content', 'filter_the_content_in_the_main_loop' );
function filter_the_content_in_the_main_loop( $content ) {
// Check if we're inside the main loop in a single post page.
if ( is_single() && in_the_loop() && is_main_query() ) {
return $content . esc_html__("I'm filtering the content inside the main loop", "my-textdomain");
}
return $content;
}
In wordpress, the_content function display the content. There is an other function for get_the_content
Go to your page file and get the content. You can use str_replace and echo the new content after.
Example single.php :
if ( $query->have_posts() ) :
while( $query->have_posts() ) : $query->the_post();
$content = get_the_content();
$new_content = str_replace(search, replace, $content);
echo $new_content;
endwhile;
endif;
If it is not possible for you, try to use the output buffer functions. If you need use this functions, I say it and I developpe more this part. But test the solution above before.
Oh, and it exist a special community for WP where your question will more pertinent : https://wordpress.stackexchange.com/

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: custom content shows before $content

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');

Wordpress wp_link_pages top and bottom of post

I've got wp_link_pages added to the bottom of my posts using this in functions.php:
function custom_pagination( $content ) {
if( is_singular() ) {
$content .= wp_link_pages('echo=0');
}
return $content;
}
add_filter( 'the_content','custom_pagination', 1 );
However this only adds it to the bottom but I'd also like to add them to the top of the post how would I go about achieving this? I'm having to use the function method due to also using jetpack related posts (Which the links appear below it if I don't use this method).
Thanks!
Your script is appending (i.e. .=) the wp_link_pages. Instead, you could do something like:
function custom_pagination( $content ) {
if( is_singular() ) {
$content = wp_link_pages('echo=0') . $content . wp_link_pages('echo=0');
}
return $content;
}
add_filter( 'the_content','custom_pagination', 1 );

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