Add A Menu Item To A WP_Nav_Menu Navigation via PHP Function

You can use wp_update_nav_menu_item() to programmatically add a new menu item to the database. Since this change persists, you shouldn’t execute this functionality on every page – it’s best used as a one-off (though in that case it might be easier and more efficient to use WP CLI as suggested by WebElaine in the comments), … Read more

What is the `post-status` icon handle when using `@wordpress/icons`?

So after a little digging and being maybe a little bit oblivious 😅. The correct way to use dashicons within WordPress is as follows; import { Dashicon } from “@wordpress/components”; <Dashicon icon=”post-status” /> Note: This info can be found in the Developer Resources: Dashicons docs, but annoyingly it is buried at the bottom of the … Read more

How to disable blocks in Gutenberg editor for specific post type

Put this in your Theme’s functions.php file. add_filter( ‘allowed_block_types_all’, ‘rt_allowed_block_types’, 25, 2 ); function rt_allowed_block_types( $allowed_blocks, $editor_context ) { if( ‘custom_post_type’ === $editor_context->post->post_type ) { $allowed_blocks = array( ‘core/image’, ‘core/paragraph’, ‘core/heading’, ‘core/list’ ); return $allowed_blocks; } else { return; } } Here’s what it’s doing. The filter allowed_block_types_all is filtering what block types are allowed. … Read more

Theme.json: creating different sections of the color palette

No, writing as of WordPress 6.0 there is no API for defining additional palette sections. The colour palette component hardcodes 3 PaletteEdit sub-components, theme, default, and custom, where custom is for user defined colours. No dynamic fields or slotfills are provided for extending or adjusting these. Here is the current code: return ( <VStack className=”edit-site-global-styles-color-palette-panel” … Read more

WordPress can’t get image url but both the attachment and the actual file are there

Not sure how you’re using it, but wp_get_attachment_image_src() returns an array. This should work: <?php $attachmentID = 519271; $imageSizeName = “thumbnail”; $img = wp_get_attachment_image_src($attachmentID, $imageSizeName); ?> <img src=”<?php echo $img[0]; ?>” alt=”image”> An alternative to try if it isn’t working: <img src=”<?php echo wp_get_attachment_url(519271); ?>” alt=”image”> This thread discusses your issue further: wp_get_attachment_image_src always returns … Read more

How to remove https:// from shortcode generated url

That would be: return parse_url( get_site_url(), PHP_URL_HOST ) ); Explanation: get_site_url is the WP function that returns the full url (it is the function that site_url relies on). parse_url is a PHP function that splits the url into several components and returns one or more of them. In this case it returns the hostname, which … Read more