Wordpress wp_link_pages top and bottom of post - php

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

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/

how to remove read more after call the_content function in wordpress?

I want to remove read-more link after calling the_content() function of wordpress. I want only to show content of each post in a loop and read-more link is redundant.
I try this code but read-more link remains after post content:
add_filter( 'the_content_more_link', 'disable_more_link', 10, 2 );
function disable_more_link( $more_link, $more_link_text ) {
return;
}
where I can find the code that adds this read-more link after the content?
You can use a filter to remove the read-more link from content. I have updated your filter. Please try it, It's working fine.
function disable_more_link( $link ) {
$link = preg_replace( '|#more-[0-9]+|', '', '' );
return $link;
}
add_filter( 'the_content_more_link', 'disable_more_link', 10, 2 );
More info
function new_excerpt_more($more) {
return '...';
}
add_filter('excerpt_more', 'new_excerpt_more');

render Visual Composer shortcodes onto page

I am trying to echo visual composer shortcodes onto a page.
I've tried both methods below, but they don't work:
functions.php:
Method 1
/*
* add shortcode file
*/
function include_file($atts) {
$a = shortcode_atts( array(
'slug' => 'NULL',
), $atts );
if($slug != 'NULL'){
ob_start();
get_template_part($a['slug']);
return ob_get_clean();
}
}
add_shortcode('include', 'include_file');
Method 2
function someshortocode_callback( $atts = array(), $content = null ) {
$output = "[vc_section full_width=\"stretch_row\" css=\".vc_custom_1499155244783{padding-top: 8vh !important;padding-bottom: 5vh !important;background-color: #f7f7f7 !important;}\"][vc_row 0=\"\"][vc_column offset=\"vc_col-lg-offset-3 vc_col-lg-6 vc_col-md-offset-3 vc_col-md-6\"][/vc_column][/vc_row][/vc_section]";
return $output;
}
add_shortcode('someshortocode', 'someshortocode_callback');
file_rendering_vc_shortcodes.php:
Method 1
<?php if ( is_plugin_active( 'js_composer/js_composer.php' ) ) {
wc_print_notice('js_composer plugin ACTIVE', 'notice');
echo do_shortcode('[include slug="vc_templates/shop-page"]');
}; ?>
Result
js_composer plugin ACTIVE
shortcode is on page with parentheses as is
Method 2
<?php $post = get_post();
if ( $post && preg_match( '/vc_row/', $post->post_content ) ) {
// Visual composer works on current page/post
wc_print_notice('VC ON', 'notice');
echo add_shortcode('someshortocode', 'someshortocode_callback');
} else {
wc_print_notice('VC OFF', 'notice');
//echo do_shortcode('[include slug="vc_templates/shop-page"]');
}; ?>
Result
VC OFF (obviously, since the vc_row in shortcode is not there)
shortcode is NOT on page
shop-page.php
<?php
/**
Template Name: Shop Page in theme
Preview Image: #
Descriptions: #
* [vc_row][vc_column][/vc_column][/vc_row]
*/
?>
[vc_section full_width="stretch_row" css=".vc_custom_1499155244783{padding-top: 8vh !important;padding-bottom: 5vh !important;background-color: #f7f7f7 !important;}"][vc_row 0=""][vc_column offset="vc_col-lg-offset-3 vc_col-lg-6 vc_col-md-offset-3 vc_col-md-6"][/vc_column][/vc_row][/vc_section]
Is it possible to render vc shortcodes on page, and if so, how is it done?
Use the:
WPBMap::addAllMappedShortcodes();
then as usual do_shortcode($content);
In short, page builder due to performance doesn't register shortcodes unless it isn't required.
If your element is registered by vc_map or vc_lean_map then no need to use add_shortcode function, you can do everything just by using WPBMap::addAllMappedShortcodes(); it is will call an shortcode class callback during the rendering process and then shortcode template.
Regarding Method 2.
You have to use do_shortcode() in your shortcode function.
function someshortocode_callback( $atts = array(), $content = null ) {
$output = '[vc_section full_width="stretch_row" css=".vc_custom_1499155244783{padding-top: 8vh !important;padding-bottom: 5vh !important;background-color: #f7f7f7 !important;}"][vc_row 0=""][vc_column offset="vc_col-lg-offset-3 vc_col-lg-6 vc_col-md-offset-3 vc_col-md-6"]column text[/vc_column][/vc_row][/vc_section]';
return do_shortcode( $output );
}
add_shortcode( 'someshortocode', 'someshortocode_callback' );
Working example on my test site: http://test.kagg.eu/46083958-2/
Page contains only [someshortocode]. Code above is added to functions.php.
In your code for Method 2 there is another error: line
echo add_shortcode('someshortocode', 'someshortocode_callback');
cannot work, as add_shortcode() returns nothing. This code should be as follows:
<?php $post = get_post();
if ( $post && preg_match( '/vc_row/', $post->post_content ) ) {
// Visual composer works on current page/post
wc_print_notice('VC ON', 'notice');
} else {
wc_print_notice('VC OFF', 'notice');
add_shortcode('someshortocode', 'someshortocode_callback');
echo do_shortcode('[someshortocode]');
}; ?>

How to make wordpress function working only on single & NOT for pages

I'm struggling with one thing. I've got such wordpress function:
function wpq_insert_attachment_data($data, $postarr){
if (!is_single() ) {
$posttitle = get_the_title( $postarr['post_parent'] );
$data['post_title'] = $posttitle;
$data['post_name'] = $posttitle;
return $data;
}}
add_filter( 'wp_insert_attachment_data', 'wpq_insert_attachment_data', 10, 2 );
It works superb but it covers all single/custom post type/pages etc. Is there any way to EXCLUDE pages from that function? I've tried sorting it out with is_single() yet without success.
Use is_singular in your statement to target specific post types.
Can also do an array to include or exclude.
If (! is_singular()) ..... or.....
(! is_singular(array('page','food',foo')))
Then it will only run on the singles for whichever post type you're targeting.
Looks like you just need to tweak your conditional statement:
if (!is_single() ) {
Should become:
if (!is_page() ) {
Load the function only on specific page, add your page id in is_page(id_here) :
function wpq_insert_attachment_data($data, $postarr){
if ( is_page(page_id)){
$posttitle = get_the_title( $postarr['post_parent'] );
$data['post_title'] = $posttitle;
$data['post_name'] = $posttitle;
return $data;
}
}
add_filter( 'wp_insert_attachment_data', 'wpq_insert_attachment_data', 10, 2 );
you can also add your page slug instead of id like :
if ( is_page('slug'))
Or exclude page(s)
if ( !is_page('slug'))
if ( !is_page(array('slug-1', 'slug-2') )

Why does the_title() filter is also applied in menu title?

I have created below function to hide page title. But when I execute this code, it also hides the menu name.
function wsits_post_page_title( $title ) {
if( is_admin())
return $title;
$selected_type = get_option('wsits_page_show_hide');
if(!is_array($selected_type)) return $title;
if ( ( in_array(get_post_type(), $selected_type ) ) && get_option('wsits_page_show_hide') )
{
$title = '';
}
return $title;
}
add_filter( 'the_title', array($this, 'wsits_post_page_title') );
Nikola is correct:
Because menu items also have titles and they need to be filtered :).
To make this only call in the posts, and not in menus, you can add a check for in_the_loop() - if it is true, you're in a post.
So change the first line in the function to:
if( is_admin() || !in_the_loop() )
and all should be well.
It's a bit of a hack but you can solve this by adding your action to loop_start.
function make_custom_title( $title, $id ) {
// Your Code Here
}
function set_custom_title() {
add_filter( 'the_title', 'make_custom_title', 10, 2 );
}
add_action( 'loop_start', 'set_custom_title' );
By embedding the_title filter inside of a loop_start action, we avoid overwriting the menu title attributes.
You can do something like that :
In your function.php :
add_filter( 'the_title', 'ze_title');
function ze_title($a) {
global $dontTouch;
if(!$dontTouch && !is_admin())
$a = someChange($a);
return $a;
}
In your template :
$dontTouch = 1;
wp_nav_menu( array('menu' => 'MyMenu') );
$dontTouch = 0;
Posting this answer because it was the search result I ended up clicking on while searching about targeting the filter hook the_title while ignoring the filter effect for navigation items.
I was working on a section in a theme which I wanted to add buttons to the page title within the heading one tag.
It looked similar to this:
<?php echo '<h1>' . apply_filters( 'the_title', $post->post_title ) . '</h1>'.PHP_EOL; ?>
I was then "hooking in" like this:
add_filter( 'the_title', 'my_callback_function' );
However, the above targets literally everything which calls the_title filter hook, and this includes navigation items.
I changed the filter hook definition like this:
<?php echo '<h1>' . apply_filters( 'the_title', $post->post_title, $post->ID, true ) . '</h1>'.PHP_EOL; ?>
Pretty much every call to the_title filter passes parameter 1 as the $post->post_title and parameter 2 as the $post->ID. Search the WordPress core code for apply_filters( 'the_title'* and you'll see for yourself.
So I decided to add a third parameter for situations where I want to target specific items which call the_title filter. This way, I can still receive the benefit of all callbacks which apply to the_title filter hook by default, while also having the ability to semi-uniquely target items that use the_title filter hook with the third parameter.
It's a simple boolean parameter:
/**
* #param String $title
* #param Int $object_id
* #param bool $theme
*
* #return mixed
*/
function filter_the_title( String $title = null, Int $object_id = null, Bool $theme = false ) {
if( ! $object_id ){
return $title;
}
if( ! $theme ){
return $title;
}
// your code here...
return $title;
}
add_filter( 'the_title', 'filter_the_title', 10, 3 );
Label the variables however you want. This is what worked for me, and it does exactly what I need it to do. This answer may not be 100% relevant to the question asked, but this is where I arrived while searching to solve this problem. Hope this helps someone in a similar situation.
The global $dontTouch; solution didn't work for me for some reason. So I simply removed the filter around the menu thus in header.php:
remove_filter( 'the_title', 'change_title' );
get_template_part( 'template-parts/navigation/navigation', 'top' );
add_filter( 'the_title', 'change_title' );
And all is well.
I think you're looking for this:
function change_title($title) {
if( in_the_loop() && !is_archive() ) { // This will skip the menu items and the archive titles
return $new_title;
}
return $title;
}
add_filter('the_title', array($this, 'change_title'), 10, 2);

Categories