I'm looking for a way to hook into an Elementor Posts Widget to display an extra H2 tag under the posts title for each posts.
I would then get this H2 value from from the single posts ACF field.
From what I am reading else where there are ways to get the whole HTML of the output as string, but that requires a lot of string replace and so not very future proof. Eg:
Hook into elementor widget?
https://developers.elementor.com/docs/hooks/render-widget-content/
If I am using a code like this is there a way to hook this after the Post title? or is string replace the best way to approach this?
function change_heading_widget_content( $widget_content, $widget ) {
if ( 'posts' === $widget->get_name() ) {
$settings = $widget->get_settings();
$post_id = "Somehow get the post id (maybe look for in the $widget_content string per post?)";
if ( ! empty( $settings['link']['is_external'] ) ) {
$widget_content .= '<h2>'. get_field("extra_heading", $post_id) .'<h2>';
}
}
return $widget_content;
}
add_filter( 'elementor/widget/render_content', 'change_heading_widget_content', 10, 2 );
I appreciate all and any help.
Thanks
If you dig into Elementor Pro source code you'll find a good hint:
Dynamic Tags -> ACF Module
get_queried_object()
Or try this:
Dynamic Tags -> ACF Module Rendering
function get_queried_object_meta( $meta_key ) {
$value = '';
if ( is_singular() ) {
$value = get_post_meta( get_the_ID(), $meta_key, true );
} elseif ( is_tax() || is_category() || is_tag() ) {
$value = get_term_meta( get_queried_object_id(), $meta_key, true );
}
return $value;
}
Or just use get_field('my-field') without $post_id
Related
I'm creating a plugin that makes a few changes to the product pages in woocommerce and i'd like to add a checkbox if the user wants to disable it for certain products.
I managed to insert the checkbox on the product edit page, but it's possible to verify if it's checked before loading the plugin?
I tried like this, but nothing happens:
global $product;
if ($product) {
$customAttributes = get_post_meta( $product->get_id(), 'custom_attributes', true );
if ( isset($customAttributes['disabled']) && $customAttributes['disabled'] == 1 ) {
return;
}
}
I searched a lot and tried other things, but i couldn't find a solution.
I always create a plugin and put it in the mu-plugins folder so you can do the check before any plugin is loaded.
Could you try the option_active_plugins hook?
add_filter( 'option_active_plugins', function( $plugins ){
global $product;
if ( $product ) {
$customAttributes = get_post_meta( $product->get_id(), 'custom_attributes', true );
if ( isset( $customAttributes['disabled'] ) && $customAttributes['disabled'] == 1 ) {
$unload_plugins[] = "my-plugins/my-plugin.php";
$plugins = array_diff( $plugins, $unload_plugins );
}
}
return $plugins;
} );
If needed, maybe wrap it in a wp or init action, but get_post_meta should work there.
I am creating a new WordPress Theme and would like to use the Classic Editor specifically on the front page. To achieve this, I plan to modify the functions.php file with a few lines of code instead of relying on a plugin.
I would like to maintain the Gutenberg editor for other pages and posts.
Despite trying different methods, I have not found a solution that worked. Or I am able to remove the Gutenberg Editor from all the pages, or not remove it at all.
That's my most recent try.
Does someone know what can I do?
Thanks a lot!!!
function use_classic_editor_on_front_page( $use_block_editor, $post_type ) {
if ( ! is_admin() && is_front_page() && 'page' === $post_type ) {
return false;
}
return $use_block_editor;
}
add_filter( 'use_block_editor_for_post_type', 'use_classic_editor_on_front_page', 10, 2 );
hope this helps,
add_filter( 'use_block_editor_for_post_type', 'use_classic_editor_on_front_page' );
function use_classic_editor_on_front_page( $use_block_editor ) {
if ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) && get_the_ID() == get_option( 'page_on_front' ) ) {
$use_block_editor = false;
}
return $use_block_editor;
}
I am trying to manipulate an elementor query for all post types. I am using the Profile Builder plugin to restrict content. Their support sent me this function to check wether the post is restricted or not:
wppb_content_restriction_is_post_restricted( $post_id )
When I output the result of this function on the post it is either 1 or nothing. (1 being restricted).
The support also send me a custom addon-plugin of another plugin they have, where I'd only need to add the new function. When I do this and install it to my page, nothing changes though.
Here is the code of their mini plugin:
<?php
/*
Plugin Name: Paid Member Subscriptions - Exclude Restricted Posts From Query
Plugin URI: http://www.cozmoslabs.com
Description: Exclude restricted posts and pages from the main queries like blog, archive and taxonomy pages. It does not exclude them from custom queries.
Author: Cristian Antohe
Version: 1.0
Author URI: http://www.cozmoslabs.com
*/
add_action( 'pre_get_posts', 'pmsc_exclude_post_from_query' );
function pmsc_exclude_post_from_query( $query ) {
remove_action('pre_get_posts', 'pmsc_exclude_post_from_query');
if( !function_exists( 'pms_is_post_restricted' ) || is_admin() || is_single() )
return;
if( $query->is_main_query() || ( $query->is_search() && isset( $_GET['s'] ) ) ) {
$args = $query->query_vars;
$args['suppress_filters'] = true;
$args['posts_per_page'] = get_option( 'posts_per_page' );
$posts = get_posts($args);
$ids = wp_list_pluck( $posts, 'ID' );
$restricted_ids = array_filter($ids,'pms_is_post_restricted');
$query->set( 'post__not_in', $restricted_ids );
}
}
In case that a post is restricted, I want to exclude it from my query. So that the user only sees posts that can be accessed.
Now the challenge is to write a custom query that I can then put inside of the query ID of elementor's post widget to perform my custom query.
My query looks like this so far:
function prevent_restricted_posts_from_loading( $query ) {
if( !function_exists( 'wppb_content_restriction_is_post_restricted' ) || is_admin() || is_single() )
return;
if( $query->is_main_query() || ( $query->is_search() && isset( $_GET['s'] ) ) ) {
$args = $query->query_vars;
$posts = get_posts($args);
$ids = wp_list_pluck( $posts, 'ID' );
$restricted_ids = array_filter($ids,'wppb_content_restriction_is_post_restricted');
$query->set( 'post__not_in', $restricted_ids );
}
}
add_action( 'elementor/query/prevent_restricted_posts_from_loading', 'prevent_restricted_posts_from_loading' );
I am trying to figure out how to replace "woocommerce_page_title" with an ACF field? If there isn't one then it should fall back to "woocommerce_page_title();". I can't seem to find anything super helpful. Your help would be greatly appreciated.
add_filter( 'woocommerce_page_title', 'custom_title' );
function custom_title( $title ) {
$title = get_field("custom_tag_h1");
return $title;
}
For Archives pages taxonomy titles and ACF:
The woocommerce_page_title works for shop page and taxonomy archive pages, so in ACF:
Then in a product category for example (here "Clothing"), you set your custom title:
In the code, you need to get the queried object (the WP_Term object) for the taxonomy archive pages and to set it as following:
add_filter( 'woocommerce_page_title', 'custom_title' );
function custom_title( $page_title ) {
if ( ! function_exists('get_field') || is_search() )
return $page_title;
if ( is_tax() ) {
$term = get_queried_object();
$the_id = $term->taxonomy . '_' . $term->term_id;
} elseif ( is_shop() ) {
$the_id = wc_get_page_id( 'shop' );
}
return get_field( "custom_tag_h1", $the_id ) ? get_field( "custom_tag_h1", $the_id ) : $page_title;
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
I have following problem in Wordpress. I have this code in function.php which should make posts title on frontend from Yoast SEO title. Code works, however it has also impact on pages.
function set_my_seo_title($title, $id)
{
global $post;
$seo_title=get_post_meta($id, '_yoast_wpseo_title', true);
return ((!empty($seo_title)&&$post->post_type=='post') ? $seo_title : $title);
}
add_filter('the_title', 'set_my_seo_title', 15, 2);
The problem is with condition post_type=='post'. It looks like there is some bug in Wordpress, but may be I am doing something wrong.
Important: I have to admit I am not good developer. This code is from other question. If you know solution for this please post your full code variant. Thank you in advance.
You could try get_post_type() instead...
function set_my_seo_title( $title, $id ) {
$seo_title = get_post_meta( $id, '_yoast_wpseo_title', true );
return ( ( ! empty( $seo_title ) && get_post_type( $id ) == 'post' ) ? $seo_title : $title );
}
add_filter( 'the_title', 'set_my_seo_title', 15, 2 );