How to add a button or box below Post Title? - wordpress

I'm writing a Wordpress plug-in that will add a button (or any HTML element) below the post title (or above if below is not possible). How can I do that?

You want to use the filter responsable for printing titles. I'm not sure exactly which one it is, but here's the function (it's likely single_post_title as demonstrated):
add_filter( 'single_post_title', 'the_post_title', 10, 2 );
function my_more_link( $post_title ) {
$button_code = "your_button_HTML_here";
return str_replace( $post_title, $post_title . $button_code );
}

Related

Add blocks to custom location on shop page?

On my woocommerce shop page I want to remove the description/blocks from the all-in-one woocommerce product grid block. Then add them back above it separately for layout reasons.
So I've removed the description like this:
remove_action( 'woocommerce_archive_description', 'woocommerce_product_archive_description', 10 );
Then I thought I could just add it back in with a shortcode block in the archive-product.html template.
add_shortcode('shop_description', 'woocommerce_product_archive_description');
The problem is, this adds it to the very top of document even before the <html> tag, instead of where I put the shortcode block...
I realised the woocommerce_product_archive_description function uses echo and to make it work with a shortcode it needs to instead use return.
So I made a new function for the shortcode and boiled it down to be:
$shop_page = get_post( wc_get_page_id( 'shop' ) );
$allowed_html = wp_kses_allowed_html( 'post' );
$description = wc_format_content( wp_kses( $shop_page->post_content, $allowed_html ) );
if ( $description ) {
return $description;
}

How to use "has_term" correctly

Im working with the single product page and I need to have a different image (depending on the category) after the add to cart button.
I tried this code but it doesn't show me the image that I need
add_action ( 'woocommerce_after_add_to_cart_button', 'content_after_button' );
function content_after_button() {
if (has_term( 'Categoria', 'Accesorios' ) ) {
echo 'https://prueba.soygorrion.com.ar/wp-content/uploads/2019/08/iconos2.jpg';
}
I think im using has_term in the wrong way.
What im trying to accomplish is:
I have a parent category that is "Accesorios" and inside that I have other child categories like "Billeteras". For each one of this child categories it has to show a diferent image.
thank you
First of all there is issue with your has_term checking. If you check has_term docs, you can find it takes first parameter as category term and second parameter as taxonomy.
Add secondly you are just echo image url that is not going to display image.
So, do as follows -
add_action ( 'woocommerce_after_add_to_cart_button', 'content_after_button' );
function content_after_button() {
// do check with has_term( $term = '', $taxonomy = '', $post = null )
if( has_term( 'Accesorios', 'product_cat' ) ) {
echo '<img src="https://prueba.soygorrion.com.ar/wp-content/uploads/2019/08/iconos2.jpg" />';
}
}
Since you mentioned that the problem is with has_term function. If you can get the product ID inside add to cart, then you can use this code to get categories and check them:
$categories = wp_get_post_terms($product_id, 'product_cat');
if(in_array("Accesorios", $categories))
{
echo "bla bla";
}
I have tested this code for another purpose, it works fine. I hope this will help. Please inform me when you test it and if you face any errors update your question with your new code.

How to show some html after featured image?

I have a related post code and I wants place it just after featured image on single post page. I have tried,
add_action( 'loop_start', 'wcr_related_posts', 10, 0 ) ;
but it doesn't work as I want to show content. It does show content at about proper place but the sidebar is not moving, just content is being shown a bit below and I also want the sidebar to come below due to code that wcr_related_posts generates.
I have not been able to find a hook that actually works that way I want it to.
You can do this using the 'the_content' filter:
add_filter( 'the_content', 'insert_featured_image', 20 );
function insert_featured_image( $content ) {
$content = preg_replace( "/<\/p>/", "</p>" . get_the_post_thumbnail($post->ID, 'post-single'), $content, 1 );
return $content;
}
Get the more details and examples as follow link : https://wordpress.stackexchange.com/questions/61272/how-do-i-add-the-featured-image-to-the-content-after-the-first-paragraph

Override the gallery shortcode (WordPress) if has a given parameter

I want to "override" the default "gallery" shortcode (WordPress), but only if I have used a given parameter to that gallery shortcode.
For example:
[gallery ids="1,2,3"]
It has no parameter, so it will output the standard gallery code.
[gallery mode="custom" ids="1,2,3"]
It has my "mode" parameter, so it will output another shortcode.
To achieve it, I have created a "gallery" shortcode in functions.php file:
function get_new_gallery( $atts ) {
extract( shortcode_atts( array(
'mode' => '',
'ids' => '',
), $atts ) );
$code ="";
if ($mode == "custom") {
//* Output custom shortcode
$code = '[custom_gallery ids="' . $ids . '"]';
} else {
//* Need to do nothing...but don't know how to do it
$code = '[gallery ids="' . $ids . '"]'; /* Here's the problem, it causes a loop */
}
return do_shortcode($code);
}
add_shortcode( 'gallery', 'get_new_gallery' );
It works fine when I use the mode="custom" parameter. It just output the new shortcode: [custom_gallery...]
However, when not using the parameter it breaks because it enters in an infinite loop. In the code, there's a comment with the line that breaks it.
What I want is to execute the standard "gallery" shortcode if no parameter is entered. But given I've overwritten it...don't know how to "scape" from the loop and just execute the gallery.
Any help?
Thanks in advance.
Maybe an alternative approach can help? What about a filter on the gallery shortcode. See references:
https://codex.wordpress.org/Plugin_API/Filter_Reference/post_gallery
and:
https://wpbeaches.com/filtering-gallery-image-output-wordpress/

xprofile_allowed_tags not accepting unordered list

Im trying to get my Budypress site to work with wp_editor on profile fields. So far everything is ok but my tags are being striped.
I added the following:
add_filter( 'xprofile_allowed_tags', 'custom_xprofile_allowed_tags' );
function custom_xprofile_allowed_tags($tags){
$tags['li'] = array();
$tags['ul'] = array(
'type' => true
);
return $tags;
}
But it still saving my profile field without <ul><li>
I know the filter is working because if i add unset($tags['strong']); the strong tag is stripped.
Thanks for the help
My code was working fine i didn't realize that i also need to change filter for displaying the data.
So my code is :
function xprofile_with_html() {
//change allowed tags to use the same as posts when save
add_filter( 'xprofile_allowed_tags', 'custom_xprofile_allowed_tags',0 );
//remove wp_filter and add custom one when showing in edit field
remove_filter( 'bp_get_the_profile_field_edit_value', 'wp_filter_kses', 1 );
add_filter( 'bp_get_the_profile_field_edit_value', 'my_custom_profile_filter',0,3 );
}
add_action( 'bp_init', 'xprofile_with_html' );
function custom_xprofile_allowed_tags($tags){
global $allowedposttags;
return $allowedposttags;
}
function my_custom_profile_filter($data){
return xprofile_filter_kses($data);
}
With these i can use wp_editor for my xprofile fields

Resources