WordPress author filters - wordpress

I have added a filter on "the_author" to add some html code with author info. However all my HTML is coming under an anchor of author link. For eg:
add_filter( 'the_author', 'myfun');
function myfun( $content ) {
$content.= 'hi';
return $content;
}
Now "Hi" is just a simple text which should not be included in author anchor tag. Please help.

add_filter( 'the_author', 'myfun' );
add_filter( 'get_the_author_display_name', 'myfun' );
function myfun( $name) {
global $post;
if ( $name)
$name.= '</br>hi';
return $name;
}

Related

How to add a custom class to an element in a WordPress post using WP hooks via Snippets?

I'm trying to add a single class (my-class) to an element in a WordPress post (LearnDash Theme). I fount out that it can be done using a hook via add_filter, but no how I could figure it out.
The sample code that I found looks like this:
add_filter( 'body_class', 'custom_body_class' );
/**
* Add custom field body class(es) to the body classes.
*
* It accepts values from a per-page custom field, and only outputs when viewing a singular static Page.
*
* #param array $classes Existing body classes.
* #return array Amended body classes.
*/
function custom_body_class( array $classes ) {
$new_class = is_page() ? get_post_meta( get_the_ID(), 'body_class', true ) : null;
if ( $new_class ) {
$classes[] = $new_class;
}
return $classes;
}
In this sample code the class is added to pages only but I need to add it to "posts". I tried is_post() but it didn't work out. I even tried it on a page, but didn't work also.
In my case I want to add a class to an element with ID learndash-page-content.
Existing code:
<div id="learndash-page-content">...</div>
What I'm struggling to do:
<div id="learndash-page-content" class="my-class">...</div>
What do I need to do to get it working?
If that is indeed the syntax of the element, you might want to try string manipulation on the output (which you will capture like this:)
function start_modify_html() {
ob_start();
}
function end_modify_html() {
$html = ob_get_clean();
$html = str_replace( '<div id="learndash-page-content">', '<div id="learndash-page-content" class="my-class">', $html );
echo $html;
}
add_action( 'wp_head', 'start_modify_html' );
add_action( 'wp_footer', 'end_modify_html' );

Popular code for removing url field in comment form doesn't work anymore

I've been using this code (copy below) to remove url field from comment form in WordPress. It doesn't seem to work anymore. Why?
function crunchify_disable_comment_url($fields) {
unset($fields['url']);
return $fields;
}
add_filter('comment_form_default_fields','crunchify_disable_comment_url');
Following code worked for me:
add_filter( 'comment_form_defaults', 'remove_url' );
function remove_url( $fields ) {
if ( isset( $fields['fields']['url'] ) )
unset( $fields['fields']['url'] );
return $fields;
}

override all in one seo pack keyword on custom page

my code is not working on custom page?
is aioseop_keywords hook wrong?
What is suggestion?
add_filter( 'aioseop_keywords', 'my_custom_keyword' );
function my_custom_keyword ( $keywords ) {
$keywords = '1,2,3,name,or,other';
return $keywords;
}

Is there any action in wordpress that points above of posts?

I'm Trying to write a wordpress plugin but i cannot find an action or Way to point above of posts
Try this:(in functions.php)
add_filter( 'the_content', 'featured_image_before_content' );
function featured_image_before_content( $content ) {
/* if ( is_singular('post') && has_post_thumbnail()) { */
$content = 'YOUR DATA'. $content;
/* }*/
return $content;
}
you can add condition from here:LINK

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.

Resources