I have a simple shortcode which i use on post titles:
add_shortcode('year', 'year_shortcode');
function year_shortcode() {
$year = date('Y');
return $year;
}
add_filter( 'single_post_title', 'my_shortcode_title' );
add_filter( 'the_title', 'my_shortcode_title' );
add_filter('wpseo_title', 'my_shortcode_title');
add_filter( 'wpseo_metadesc', 'my_shortcode_title' );
function my_shortcode_title( $title ){
return do_shortcode( $title );
}
It works fine but I suddenly realized that Yoast creates an og:title and it doesn't render there (meaning it doesn't render on Facebook or whatsapp)
I searched for an answer and couldn't find anythinng.
Has anyone faced this before?
Maybe I need to run the og:title through a filter of some sort which is also fine, the question is how.
Thanks in advance.
I used this and it mostly works.
It will update the og:title, and socials etc in Yoast.
It does not update Yoast's default Schema output. This would probably need to be addressed by them.
// Create Year shortcode to replace with current year.
add_shortcode( 'currentyear', 'current_year' );
function current_year() {
$year = date( 'Y' );
return $year;
}
// Activate shortcode function in Post Title.
add_filter( 'the_title', 'do_shortcode' );
add_filter( 'single_post_title', 'do_shortcode' );
add_filter( 'wpseo_title', 'do_shortcode' );
add_filter( 'wpseo_metadesc', 'do_shortcode' );
add_filter( 'wpseo_opengraph_title', 'do_shortcode' );
add_filter( 'wpseo_opengraph_desc', 'do_shortcode' );
add_filter( 'wpseo_opengraph_site_name', 'do_shortcode' );
add_filter( 'wpseo_twitter_title', 'do_shortcode' );
add_filter( 'wpseo_twitter_description', 'do_shortcode' );
add_filter( 'the_excerpt', 'do_shortcode' );
Related
I need to add a text mention after prices (sub-total, total...) displayed in my cart page and checkout page.
I know how to do that on single page and shop page but not on the other pages. The only thing I found is this code, but it does not work.
add_filter( 'woocommerce_get_price_html', 'custom_price_message' );
function custom_price_message( $price ) {
$text = 'lorem';
return $price . $text;
}
I found the answer but it does not add to shipping price:
add_filter( 'woocommerce_cart_item_price', 'kd_custom_price_message' );
add_filter( 'woocommerce_cart_item_subtotal', 'kd_custom_price_message' );
add_filter( 'woocommerce_cart_subtotal', 'kd_custom_price_message' );
add_filter( 'woocommerce_cart_total', 'kd_custom_price_message' );
function kd_custom_price_message( $price ) {
$afterPriceSymbol = ' TTC';
return $price . $afterPriceSymbol;
}
For shipping price :
add_filter( 'woocommerce_cart_shipping_method_full_label', 'kd_custom_price_message' );
add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );
function cw_change_product_price_display( $price ) {
$text = __(' X ');
return $price . ' ' . $text;
}
For cart page you can try this code.
I am building a custom webshop and I am trying to remove the photoswipe style documents but I can't seem to get rid of them, I tried a couple of "solutions" that I found online but they are not working. This is what I tried so far in the functions.php:
remove_theme_support( 'wc-product-gallery-zoom' );
remove_theme_support( 'wc-product-gallery-lightbox' );
remove_theme_support( 'wc-product-gallery-slider' );
and
add_filter( 'woocommerce_enqueue_styles', 'wpf_dequeue_styles' );
function wpf_dequeue_styles( $enqueue_styles ) {
unset( $enqueue_styles['photoswipe-css'] );
unset( $enqueue_styles['photoswipe-default-skin-css'] );
return $enqueue_styles;
}
and
wp_dequeue_script('photoswipe-css');
wp_dequeue_script('photoswipe-default-skin-css');
Can someone help me finding the solution? I don't have any images in my webshop so I do not need these scripts.
You can remove_theme_support using after_setup_theme action hook.
add_action( 'after_setup_theme', 'remove_photoswipe_css', 11 );
function remove_photoswipe_css() {
remove_theme_support( 'wc-product-gallery-zoom' );
remove_theme_support( 'wc-product-gallery-lightbox' );
remove_theme_support( 'wc-product-gallery-slider' );
}
You can wp_dequeue_style using wp_enqueue_scripts action hook.
add_action( 'wp_enqueue_scripts', 'dequeue_photoswipe_css', 11 );
function dequeue_photoswipe_css() {
wp_dequeue_style( 'photoswipe-css' );
wp_dequeue_style( 'photoswipe-default-skin-css' );
}
If anyone is still looking for the answer to this then the correct code would be :
add_action( 'wp_enqueue_scripts', 'dequeue_photoswipe_css', 99);
function dequeue_photoswipe_css() {
wp_dequeue_style( 'photoswipe' );
wp_dequeue_style( 'photoswipe-default-skin-css' );
wp_dequeue_script( 'photoswipe-ui-default' );
}
I have try remove action but not working.
add_action( 'wp_loaded', 'remove_my_action' );
function remove_my_action() {
remove_action( 'wp_head', 'gutenberg_render_title_tag', -1 );
}
or
add_action( 'init', 'remove_my_action' );
function remove_my_action() {
remove_action( 'wp_head', 'gutenberg_render_title_tag', -1 );
}
i am using yoast plugin. Yoast plugin also using remove action https://github.com/Yoast/wordpress-seo/blob/trunk/src/integrations/front-end-integration.php#L220 but it's not working. Still tag coming two time. i wants to remove gutenberg title tag.
I got the solution for this.
add_action( 'wp', 'remove_default_guten_title' );
function remove_default_guten_title() {
foreach ( gutenberg_get_template_type_slugs() as $template_type ) {
add_filter( str_replace( '-', '', $template_type ) . '_template', 'remove_default_title', 21, 3 );
}
}
function remove_default_title() {
remove_action( 'wp_head', 'gutenberg_render_title_tag', 1 );
return gutenberg_dir_path() . 'lib/template-canvas.php';
}
I want to show the css and javascript only when the shortcode is used in that page. If the short code not present in the wordpress page then the js and css of contact form should not be shown. For that what i have done is i have pasted the following code in my active themes function.php file.
add_filter( 'wpcf7_load_js', '__return_false' );
add_filter( 'wpcf7_load_css', '__return_false' );
The above code totally removes the js and css of contact form 7 plugin. What i need is if contact form 7 shortcode is pasted then both should be shown.
Here is the answer for your question. If there is not shortcode the css and js of contact form will be removed and if there is shortcode css and js will be added.
function rjs_lwp_contactform_css_js() {
global $post;
if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'contact-form-7') ) {
wp_enqueue_script('contact-form-7');
wp_enqueue_style('contact-form-7');
}else{
wp_dequeue_script( 'contact-form-7' );
wp_dequeue_style( 'contact-form-7' );
}
}
add_action( 'wp_enqueue_scripts', 'rjs_lwp_contactform_css_js');
I needed the other version that corresponds widgets and shortcodes in theme file.
add_filter( 'wpcf7_load_css', '__return_false' );
add_filter( 'wpcf7_load_js', '__return_false' );
remove_action( 'wp_enqueue_scripts','wpcf7_recaptcha_enqueue_scripts', 20 );
add_filter('pre_do_shortcode_tag', 'enqueue_wpcf7_css_js_as_needed', 10, 2 );
function enqueue_wpcf7_css_js_as_needed ( $output, $shortcode ) {
if ( 'contact-form-7' == $shortcode ) {
wpcf7_recaptcha_enqueue_scripts();
wpcf7_enqueue_scripts();
wpcf7_enqueue_styles();
}
return $output;
}
You use below code.You can add your pages Id in this code.
function dvk_dequeue_scripts() {
$load_scripts = false;
if( is_singular() ) {
$post = get_post();
if( has_shortcode($post->post_content, 'contact-form-7') ) {
$load_scripts = true;
}
}
if( ! $load_scripts ) {
wp_dequeue_script( 'contact-form-7' );
wp_dequeue_style( 'contact-form-7' );
}
}
add_action( 'wp_enqueue_scripts', 'dvk_dequeue_scripts', 99 );
Reference
I have installed the wordpress SEO Yoast plugin on my blog. I want to avoid using SEO Yoast generated title for my blog. Is there any way I can remove filter that are being applied by SEO Yoast plugin for wp_title()?
I want to use the page title as SEO title.
add_filter( 'wp_title', 'te_before_filter_wp_title' );
function te_before_filter_wp_title( $title ) {
echo $title;
//exit;
return $title;
}
add_filter( 'wp_title', 'te_after_filter_wp_title' ,9999);
function te_after_filter_wp_title( $title ) {
echo $title;
//exit;
return $title;
}
I have write this code to test the title. And title in te_before_filter_wp_title and in te_after_filter_wp_title is different. So it is being modified by SEO YOAST plugin.
With older versions of WPSEO (v1~2), you can remove the filter it applies with the following code:
add_action( 'init', function() {
global $wpseo_front;
remove_filter( 'wp_title', array( $wpseo_front, 'title' ), 15 );
}, 20 );
With newer versions of WPSEO (~3+) and WordPress (4.4+), the filter(s) can be removed with the following code:
add_action( 'init', function () {
$wpseo_front = WPSEO_Frontend::get_instance();
remove_filter( 'pre_get_document_title', array( $wpseo_front, 'title' ), 15 );
remove_filter( 'wp_title', array( $wpseo_front, 'title' ), 15 );
} );