Wordpress: admin_footer_text text not appended - wordpress

I'm trying to use admin_footer_text in my plugin to add html into admin page. it works. but I've another plugin install that's using the same hook admin_footer_text. my plugin is overwriting the html code of previous one. I want it to append not to overwrite.
here is how I'm using the code
add_filter('admin_footer_text', 'my_footer_admin', 1);
function my_footer_admin($default){
ob_start();
echo 'some text';
$content = ob_get_clean();
return $content;
}

You need to do it like the following example
add_filter('admin_footer_text', 'my_footer_admin', 1);
function my_footer_admin( $default ) {
ob_start();
echo 'some text';
$content = ob_get_clean();
return $default $content;
}

Related

Why is the filter the_title not working when the_content is working fine?

Function and filter to display a player post titles:
function wpb_after_post_content($content){
if (is_single()) {
$id=get_the_ID();
$post_meta=get_post_meta(get_the_ID(), 'enclosure', TRUE);
$mp3=explode('.mp3',$post_meta);
$title=get_the_title();
if(count($mp3)>1)
{
$player = '<div class="post_player">' . do_shortcode('[fap_track url="'.trim($mp3[0]).'.mp3" title=url="'.$title.'" share="" cover="" meta="" layout="list" enqueue=no auto_enqueue=yes]')."</div>";
$content=$player.$content;
}
}
if (is_single()) {
$id=get_the_ID();
$post_meta=get_post_meta(get_the_ID(), 'enclosure', TRUE);
$mp3=explode('.mp3',$post_meta);
$title=get_the_title();
if(count($mp3)>1)
{
$player = '<div class="zoomsounds_player">'. do_shortcode('[zoomsounds_player source="'.trim($mp3[0]).'.mp3" play_in_footer_player="on" config="default" artistname="'.get_post_meta($id, 'arxiu_de_so', true).'" config="" ]');
/**if (wp_is_mobile())*/ {
if(get_post_meta($id, 'arxiu_de_so', true)!=""){
/** $player .= " <p id='arxiu_so'> Arxiu de so: ".get_post_meta($id, 'arxiu_de_so', true)."</p>";*/
/**$player .= '<span id="arxiuVal" style="opacity:1"> '.get_post_meta($id, 'arxiu_de_so', true).'</span>';*/
}
}
$player .="</div>";
$content=$player.$content;
}
}
return $content;
}
add_filter( "the_content", "wpb_after_post_content" );
The above code works fine with the_content filter and displays the player below the featured image. However, when I use this function with the the_title filter, the page doesn't load at all. The reason I want to use the_title is because I want the player to display above the featured image but below the post title.
Screenshot:-
This screenshot is to show that with the_content the player displays however it displays below post image.
Please have a look as below:
If you want to use the_title(), then you no need to pass the post_id in the the_title(), we have some params for the_title() if you want to add them like as below:
$before
(string) (Optional) Markup to prepend to the title.
Default value: ''
$after
(string) (Optional) Markup to append to the title.
Default value: ''
$echo
(bool) (Optional) Whether to echo or return the title. Default true for the echo.
Default value: true
If you want to use get_the_title(), then you must need to pass post_id inside the get_the_title($postId);
According to your code snippet, you must update your code with as below for get_the_title():
<?php
function wpb_after_post_content($content){
if (is_single()) {
$id=get_the_ID();
$post_meta=get_post_meta(get_the_ID(), 'enclosure', TRUE);
$mp3=explode('.mp3',$post_meta);
$title=get_the_title($id);
if(count($mp3)>1) {
$player = '<div class="post_player">' . do_shortcode('[fap_track url="'.trim($mp3[0]).'.mp3" title=url="'.$title.'" share="" cover="" meta="" layout="list" enqueue=no auto_enqueue=yes]')."</div>";
$content=$player.$content;
}
}
return $content;
}
add_filter( "the_content", "wpb_after_post_content" );
?>
And for the the_title(), you should update your code with as below:
<?php
function wpb_after_post_content($content){
if (is_single()) {
$id=get_the_ID();
$post_meta=get_post_meta(get_the_ID(), 'enclosure', TRUE);
$mp3=explode('.mp3',$post_meta);
$title=get_the_title();
if(count($mp3)>1)
{
$player = '<div class="post_player">' . do_shortcode('[fap_track url="'.trim($mp3[0]).'.mp3" title=url="'.the_title().'" share="" cover="" meta="" layout="list" enqueue=no auto_enqueue=yes]')."</div>";
$content=$player.$content;
}
}
return $content;
}
add_filter( "the_content", "wpb_after_post_content" );
?>
NOTE: You have not declared the title parameter in the zoomsounds_player shortcode so please pass and update accordingly.
I hope it would help you out.

Elementor page builder shortcode issue

I'm facing a weird problem when using the Elementor Wordpress Page Builder.
After creating a custom shortcode and inserting it into any page position, it also shows up at the top of the page, but only in Edit mode.
Top of the page:
Place where I want to insert shortcode:
This answer on an unrelated site helped me solve this Elementor issue. https://wp-types.com/forums/topic/shortcode-output-showing-up-in-the-wrong-place/
I just had to include ob_start(); and $content = ob_get_clean(); return $content; in my function. Here is what it looks like:
function custom_author_link_function() {
ob_start();
coauthors_posts_links();
$content = ob_get_clean();
return $content;
}
add_shortcode('custom_author_link', 'custom_author_link_function');
This is my working example:
function name_it( ){
ob_start();
function_name();
$content = ob_get_clean();
return $content;
return function_name();
}
add_shortcode( 'shortcode_name', 'name_it' );
Just look at function_name(); and return function_name(); lines to avoid errors.

Adding text to all posts of certain category in wordpress using MySQL

I have some blog. In one of my category i have 500 posts. I need to insert one simply link on the top of all of this posts which are for example cat=5.
How can i do it using mysql?
Thanks in advance!
you don't need to do it in mysql, you can use a hook.
Something like that:
function append_content($content) {
global $post;
if (in_category( 5, $post )) {
$original = $content;
$content = "<div class=\"ikaz\">";
$content .= "hello all";
$content .= "</div>";
$content .= $original;
return $content;
}
}
add_filter( 'append_content', 'like_content' );

Wordpress Gallery Shortcode Pregreplace

Basically I need to remove the gallery shortcode from the Wordpress content, I'm using
echo preg_replace('/\[gallery ids=[^\]]+\]/', '', get_the_content() );
It is removing the gallery shortcode successfully, but also the paragraph tags which I need to keep. The idea is that I want to output everything in the content except the gallery.
You could use Wordpress strip_shortcode function.
Look at the example in the Codex.
You can create a filter that strips shortcodes:
function remove_shortcode_from($content) {
$content = strip_shortcodes( $content );
return $content;
}
and call it when you need (in your template):
add_filter('the_content', 'remove_shortcode_from');
the_content();
remove_filter('the_content', 'remove_shortcode_from')
EDIT 1
Another way to get that (and answering you comment) you can use Wordpress apply_filters function in the content after remove the undesirables shortcodes.
//within loop
$content = get_the_content();
$content = preg_replace('/\[gallery ids=[^\]]+\]/', '', $content );
$content = apply_filters('the_content', $content );
echo $content;
But I would not recommend to you to do that. I think forcing your site to modify the content of a post could make that hard to understanding. Maybe you should work with Wordpress Excerpt and avoid any problem.
A link that helped me
to remove a shortcode or a particular list of shortcode you can use this code.
global $remove_shortcode;
/**
* Strips and Removes shortcode if exists
* #global int $remove_shortcode
* #param type $shortcodes comma seprated string, array of shortcodes
* #return content || excerpt
*/
function dot1_strip_shortcode( $shortcodes ){
global $remove_shortcode;
if(empty($shortcodes)) return;
if(!is_array($shortcodes)){
$shortcodes = explode(',', $shortcodes);
}
foreach( $shortcodes as $shortcode ){
$shortcode = trim($shortcode);
if( shortcode_exists($shortcode) ){
remove_shortcode($shortcode);
}
$remove_shortcode[$shortcode] = 1;
}
add_filter( 'the_excerpt', 'strip_shortcode' );
add_filter( 'the_content', 'strip_shortcode' );
}
function strip_shortcode( $content) {
global $shortcode_tags, $remove_shortcode;
$stack = $shortcode_tags;
$shortcode_tags = $remove_shortcode;
$content = strip_shortcodes($content);
$shortcode_tags = $stack;
return $content;
}
dot1_strip_shortcode( 'gallery' );
Accepts single, comma seprated shortcode string or array of shortcodes.

Wordpress excerpt - image and text

This is the mockup I have:
http://img697.imageshack.us/img697/3172/featuresb.jpg
Each section will be an excerpt of a post that has a certain tag.
Is there any way of doing this so a client doesnt have to touch and tags or code like that?
You could filter the content with your own function.
Instead of using <?php the_content() ?> its better to use this: <?php your_function(get_the_content()) ?>
The function can be put on functions.php and you are free to code.
You can use the_content filter like so,
add_filter('the_content', 'my_content_filter'); //
function my_content_filter($content) {
global $post;
if($post->post_excerpt == ''){ // check if the post has excerpt
$content = strip_tags($content); //strip tags
$cont_array = explode(' ',$content);
if(count($cont_array) > 55) //number of words wanted in excerpt default is 55
$content = implode(' ',array_slice($cont_array, 0, 55)).'...';
$content = '<p>'.$content.'</p>';
}else{
$content = $post->post_excerpt; //copy excerpt to content
}
return $content; //return content
}
The above code checks if the post has excerpt, if it has excerpt it returns excerpt else returns first 55 words (default length for excerpt).
use this code for limit the post content.
<?php substr($post->post_content, 0, 12); ?> ...
Put this in your themes functions.php file:
function excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'...';
} else {
$excerpt = implode(" ",$excerpt);
}
$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
return $excerpt;
}
then add this this in your loop:
<?php print '<p>'.excerpt(40).'</p>'; ?>

Resources