i have a very small problem. I hope someone could enlighten me why this is giving me this error. I have here my function that displays the url of the image of one of my category under my custom taxonomy called 'item_category'. this is under functions.php
function list_all_categories(){
$categories = get_term( 2 ,'item_category' );
$src = get_term_meta(2,'javo_item_category_featured',true);
echo $src;
}
add_shortcode( 'list','list_all_categories' );
You must update your Wordpress to have access to the get_term_meta function since you're runing version 4.2.6 but it's released on 4.4.0.
You can see it in the documentation here:
https://developer.wordpress.org/reference/functions/get_term_meta/
If you don't want to update for whatever reason, the source of the function:
function get_term_meta( $term_id, $key = '', $single = false ) {
// Bail if term meta table is not installed.
if ( get_option( 'db_version' ) < 34370 ) {
return false;
}
return get_metadata( 'term', $term_id, $key, $single );
}
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.
Need to prevent the main image on the product page from lazy loading.
The main product image is loaded in 'woocommerce/single-product/product-image.php'
It uses: wp_get_attachment_image( $attachment_id, $size, $icon, $attr ); to get the image.
Inside the function above, there is:
// Add `loading` attribute.
if ( wp_lazy_loading_enabled( 'img', 'wp_get_attachment_image' ) ) {
$default_attr['loading'] = wp_get_loading_attr_default( 'wp_get_attachment_image' );
}
$attr = wp_parse_args( $attr, $default_attr );
// If the default value of `lazy` for the `loading` attribute is overridden
// to omit the attribute for this image, ensure it is not included.
if ( array_key_exists( 'loading', $attr ) && ! $attr['loading'] ) {
unset( $attr['loading'] );
}
So clearly it's possible to not lazy load it, but I just don't fully understand how I can do this?
A way you can handle this is by creating a conditional that checks if you are currently on a single product page using the is_product() conditional provided by WooCommerce found here.
Also, instead of unsetting attr['loading'], you can set it to eager to ensure that the browser does not automatically set it as something else.
Your final code could look something like this:
function remove_lazy_load($attr, $attachment, $size){
if ( is_product() ) {
$attr['loading'] = 'eager';
}
return $attr;
}
add_filter("wp_get_attachment_image_attributes", 'remove_lazy_load', 10, 3);
edit:
I ended up actually having to implement this myself and this is the solution I came up with
function remove_lazy_load($attr, $attachment, $size){
// check if we're on the single product page
if(is_product()){
// main product image has the class wp-post-image so we'll check the attributes of the image if it contains the class
if(strpos($attr['class'], 'wp-post-image') !== false){
// you can unset but I prefer to explicitly set it
$attr['loading'] = "eager";
}
}
return $attr;
}
add_filter("wp_get_attachment_image_attributes", 'remove_lazy_load', 10, 3);
one way you might be able to do this is to use the wp_get_attachment_image_attributes filter like below:
<?php
add_filter("wp_get_attachment_image_attributes", function($attr, $attachment, $size) {
$first_image_post_id = 'ID of the image you want to remove the lazy attr';
if ( $attachment->ID === $first_image_post_id ) {
unset( $attr['loading'] );
}
return $attr;
}, 10, 3);
More info on the WP docs: https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_attributes/
add_filter( 'wp_get_attachment_image_attributes', 'remove_img_attr', 10, 2 );
function remove_img_attr( $attr, $attachment ) {
unset( $attr['id'] );
return $attr;
}
I have a custom post called ‘project’.
When a user logs in, post is automatically created.
When the post is created, I need to automatically add a featured image (only one image: number 6120) for all posts.
I tried the following code but it doesn’t add a featured image.
I’m a beginner so I’m not good at coding, would you please let me know how to solve this problem?
function wpsites_auto_set_featured_image() {
global $post, $post_type;
if( $post_type == "project" ) {
$featured_image_exists = has_post_thumbnail($post->ID);
if (!$featured_image_exists) {
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, '6120');
wp_reset_query();
}
}
}
}
}
add_action('save_post', 'wpsites_auto_set_featured_image');
Thank you.
Use save_post_{$post->post_type} for particular post type. check below code.
function wpsites_auto_set_featured_image( $post_id ) {
if ( !has_post_thumbnail( $post_id ) ) {
$thumbnail_id = 6120;
update_post_meta( $post_id, '_thumbnail_id', $thumbnail_id );
}
}
add_action( 'save_post_project', 'wpsites_auto_set_featured_image', 10 );
I'm trying to alter the behavior of the Publish to Apple New Wordpress plugin. My theme uses a custom field for video embeds but the plugin doesn't recognize that content. I'm trying to append the meta to the beginning of the posts in Apple News. This is my code that isn't working:
function add_post_meta_content($content) {
$meta = get_post_meta( get_the_ID(), 'csco_post_embed', true );
return .$meta.$content;
}
add_filter('apple_news_exporter_content_pre', add_post_meta_content);
apply_filters( 'apple_news_exporter_content_pre', $post->post_content, $post->ID );
If I for instance alter the code to the following:
function add_post_meta_content($content) {
$meta = get_post_meta( get_the_ID(), 'csco_post_embed', true );
return 'Print this content before the post'.$meta.$content;
}
add_filter('apple_news_exporter_content_pre', add_post_meta_content);
apply_filters( 'apple_news_exporter_content_pre', $post->post_content, $post->ID );
It appends "Print this content before the post" to the beginning of the post without issue. What am I missing here?
Plugin is already sending $post->ID to your filter, so no need to call get_the_ID().
Try this code:
function add_post_meta_content($content, $post_id) {
$meta = get_post_meta( $post_id, 'csco_post_embed', true );
return 'Print this content before the post'.$meta.$content;
}
add_filter('apple_news_exporter_content_pre', add_post_meta_content, 10, 2);
--
If that doesn't work, make sure that you actually have something saved in the database meta table under csco_post_embed key. Easy way to confirm this is to open your database and do a quick query:
SELECT *
FROM wp_postmeta
WHERE post_id = ENTER_YOUR_POST_ID_HERE AND meta_key = 'csco_post_embed';
Awesome! You got me 99% of the way there. Had to return the oembed to get the video to work. Here's the final code in case someone else comes looking.
function add_post_meta_content($content, $post_id) {
$meta = wp_oembed_get( get_post_meta( $post_id, 'csco_post_embed', true ) );
return $meta.$content;
}
add_filter('apple_news_exporter_content_pre', add_post_meta_content, 10, 2);
I have a function in my functions.php file and I need the current post ID.
I have tried getting it like this:
global $wp_query;
$currentID = $wp_query->post->ID;
echo '<pre>';
print_r($currentID);
echo '</pre>';
but doesn't seem to work since it says:
Trying to get property of non-object
EDIT: Entire function in functions.php
add_filter( 'gform_pre_render_1', 'populate_posts' );
add_filter( 'gform_pre_validation_1', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_1', 'populate_posts' );
add_filter( 'gform_admin_pre_render_1', 'populate_posts' );
function populate_posts( $form ) {
global $wp_query;
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos($field['cssClass'], 'booking-option') === false ) {
continue;
}
$currentID = $wp_query->post->ID;
var_dump($wp_query->post);
$choices[] = array( 'text' => $price, 'value' => $price );
$field->placeholder = '0';
$field->choices = $choices;
}
return $form;
}
Anyone can help me out please
Thanks a lot!
In your function add
global $post;
echo $post->ID;
To have access to the post id you must be within the loop
Otherwise , you must modify your function to accept the post id as a parameter , and hook where is safe to get post id, like so:
add_action('template_redirect', function() {
if (is_single())
your_function(get_queried_object_id());
}
});
function your_function($id){
//Do what you want
}
Some references:
https://wordpress.stackexchange.com/questions/177262/cant-get-post-id-in-functions-php?rq=1
https://wordpress.stackexchange.com/questions/140753/get-current-post-id-in-functions-php
As a note, for the future i think is more appropriate if you post these questions to the http://wordpress.stackexchange.com community
EDIT:
now that you posted the entire code i see that you are using gravity forms (which you didn't mentioned before).
This is a completely different question then.
You must obtain the post_id from the Entry object that gravity forms will pass to your function
https://www.gravityhelp.com/documentation/article/entry-object/
if you want to print the ID then use
the_ID();
if you want to store it then use
$postId = get_the_ID();
Use it in the loop
Hope this helps
Take Care and Happy coding
Getting the current post ID
The ID can be stored as a variable using
<?php $postid = get_the_ID(); ?>
To print
<?php echo $postid; ?>
By function
function get_the_ID() {
$post = get_post();
return ! empty( $post ) ? $post->ID : false;
}
For Reference : click here