Don’t print customizer styles when no setting has been used

There’s not really any trick, you need to check all the values. You can save a little space though, by omitting the != ”, which is redundant: if ( get_theme_mod( ‘myplugin_h2_color’ ) || get_theme_mod( ‘myplugin_h3_color’ ) || get_theme_mod( ‘myplugin_p_color’ ) || get_theme_mod( ‘myplugin_p_size’ ) ) { However, it’s possible to save Customiser settings into a … Read more

Using get_theme_mod in php ajax form doesn’t work

You should never access directly theme files, form submissions, ajax, and even just get requests will be blocked at the server level by some security hardening measure. Ignoring that for a moment, the reason your code fails is because wordpress wasn’t bootstrapped and therefor there are no core functions available. It is actually not 100% … Read more

get_theme_mod outputs number when using WP_Customize_Cropped_Image_Control

I am not very familiar with Customizer part of code, but my immediate guess would that number is attachment ID for the uploaded image. IDs are most typical and reliable way to store connection to particular media item within WP installation. It is trivial to get from ID to other representations, such as URLs to … Read more

Theme logo metadata into template file

Ends up that “alt” is not stored in wp_get_attachment_metadata so I pulled it from the post meta: $custom_logo_id = get_theme_mod(‘custom_logo’); if ($custom_logo_id) { $image = wp_get_attachment_image_src($custom_logo_id, ‘full’); $meta = wp_get_attachment_metadata($custom_logo_id); **$alt_text = get_post_meta($custom_logo_id, ‘_wp_attachment_image_alt’, true);** echo ‘<img src=”‘ . $image[0] . ‘” alt=”‘ . $alt_text . ‘” width=”‘ . $meta[‘width’] . ‘” height=”‘ . $meta[‘height’] … Read more

Issue with theme mod options during domain migration

Use the official CLI tool: wp search-replace ‘http://localhost’ ‘https://yoursitecom’ It will automatically deserialize any post meta, options, theme mods, etc and adjust them to match the new URL. Fundamentally though, it’s bad practice to store URLs to images posts and assets in the database. Store the post ID instead, and this problem goes away

Progmatically adding menu links to the default (Top) or Footer menu

You have to register the menu location first. Also, I’ve added else statement to be sure $menu_id is defined when menu exists. Otherwise, you’ll see the PHP Notice about undefined variable. WP_DEBUG must be turned on when you develop, that’s why you don’t see the notice. // Initialize name and location $menu_name=”Simple Inventory Menu”; $menu_location … Read more