How can you limit srcset on a single type of page?

Here’s an example for filtering out sizes above set limit from the srcset attribute on certain post type. This works for featured image and images added to the post content with Gutenberg. (Tested on Twenty Twenty theme). function filter_wp_calculate_image_srcset( $sources, $size_array, $image_src, $image_meta, $attachment_id ) { global $post; if ( $post && ‘post’ === $post->post_type … Read more

How to manage arrays from custom functions stored in functions.php?

A callback for an action doesn’t return anything, because that return value is never passed through by WordPress. Use a filter instead: add_filter( ‘listofnames’, ‘SomeNames’ ); function SomeNames() { $names=array( “john”,”edgar”,”miles”); return $names; } And in your template you call it like this: $names = apply_filters( ‘listofnames’, [] ); foreach ( $names a $name ) … Read more

How can I load a javascript file that is type=”module” the WordPress way?

Here is the final code I now use to add type=”module” to my scripts. wp_register_script(‘my-script’, get_theme_file_uri(‘/js/script.js’), [‘axios’], ‘1.0’); wp_enqueue_script(‘my-script’); add_filter(“script_loader_tag”, “add_module_to_my_script”, 10, 3); function add_module_to_my_script($tag, $handle, $src) { if (“my-script” === $handle) { $tag = ‘<script type=”module” src=”‘ . esc_url($src) . ‘”></script>’; } return $tag; }

How to display a default image for post thumbnail using functions.php?

The relevant code is in wp-includes/post-thumbnail-template.php. The thumbnail ID is stored in post metadata as _thumbnail_id. You could either upload the default image to WordPress and make its ID the default thumbnail ID for any post that doesn’t have one: function default_post_metadata__thumbnail_id( $value, $object_id, $meta_key, $single, $meta_type ) { if ( ‘_thumbnail_id’ == $meta_key ) … Read more

Update Content based on expiration time or queued processing

Few things come to my mind, which could help. Post status Perhaps you could use the pending post status, which you would use as a parameter in aggiorna_dati(), for the auto-generated posts that needs to be processed by funzione_recensione(). After the function is done it would change the status to something else. $args[‘post_status’] = ‘pending’; … Read more

echo get_post_meta()

Remove the echo. In PHP, you only use echo when you want to send the result as text to the browser. If you want to use the value in PHP (like you are trying to do), then you leave out the echo and add $post->ID, ‘meta_field_you_want’, true to get_post_meta();

Using “Read More” link with custom excerpt

This should remove the <p> tags, but leave the styling by replacing the paragraph tag with a <span> class: function all_excerpts_get_more_link($post_excerpt) { return ” . $post_excerpt . ” . ‘<span class=”readmore”><a href=”‘. get_permalink($post->ID) . ‘”>’ . ‘Continue Reading &raquo’ . ‘</span>’; } add_filter(‘wp_trim_excerpt’, ‘all_excerpts_get_more_link’);