All Questions (5,616 questions)



Change CSS/formatting on specific page, but not pages in general?

Posted by Prem Tiwari | Updated on

After creating the specific CSS you need to format the page how you like, you could: Create a custom page template with the CSS and assign the template this page. Create a page-{slug}.php file with the CSS, …

What action should I hook into when adding roles and capabilities?

Posted by Prem Tiwari | Updated on

When adding a role and capabilities you only need to run the code once since the roles and capabilities are saved to the database when using add_role or ->add_cap functions so just like Andy said …

How to check if a slug exists?

Posted by Prem Tiwari | Updated on

How to check if a slug exists? This is what you’re looking for, tested and I use it on my own sites: You can then use it like this:

Preserving theme settings in child theme

Posted by Prem Tiwari | Updated on

You could do something like what Genesis child themes include to save default theme settings when activating and switching themes: Clearly you would need to modify this code to work with your themes functionality.

wp-cli Enabling Maintainance Mode

Posted by Prem Tiwari | Updated on

WP-CLI now has native commands for it. See wp-cli/maintenance-mode-command for more information.

after_setup_theme always runs

Posted by Prem Tiwari | Updated on

after_setup_theme always runs SOLUTION: after_switch_theme does exactly what I intended here. It fires after the theme is switched TO your theme. One of the solutions mentioned below uses switch_theme. This does not have the desired …

How to echo the_excerpt without the P tag wrapper?

Posted by Prem Tiwari | Updated on

in your code above use get_the_excerpt() instead of the_excerpt(), because the last one will output the excerpt to the screen, and not pass it to your other functions.

Using a menu walker add a custom item at the end of the menu’s items

Posted by Prem Tiwari | Updated on

Using a menu walker add a custom item at the end of the menu’s items. You don’t need a walker in this case. A filter called wp_nav_menu_items is available. It allows you to edit the …

Is it bad practice to create own table for a plugin?

Posted by Prem Tiwari | Updated on

I rarely disagree with otherwise knowledgeable users, but in this case I can’t help it. In my opinion calling the usage of non-core database tables bad practice per se is just simply wrong. The choice …

Use WP init hook to call other hooks?

Posted by Prem Tiwari | Updated on

Use WP init hook to call other hooks? In general: Yes, wait for a dedicated hook to start your own code. Never just throw an object instance into the global namespace. But init is rarely …

Check if add_menu_page exists or not

Posted by Prem Tiwari | Updated on

You can use the fourth parameter of add_menu_page(), the my_unique_slug, to check if the page exists: $GLOBALS[‘admin_page_hooks’] is the list of registered pages.

When to use esc_html and when to use sanitize_text_field?

Posted by Prem Tiwari | Updated on

esc_html() is more or less lossless — it just turns HTML markup into encoded visible text, so that it’s not rendered as markup by browser. Semantically it’s escape, so it’s meant to be used to make output to …

Create a custom archive page for a custom post type in a plugin

Posted by Prem Tiwari | Updated on

What you need is hooking template_include filter and selectively load your template inside plugin. As a good practice, if you plan to distribute your plugin, you should check if archive-my_plugin_lesson.php (or maybe myplugin/archive-lesson.php) exists in theme, if not use the …

WordPress Get Image Description

Posted by Prem Tiwari | Updated on

WordPress Get Image Description. This is dumped into your functions.php and can then be called with $attachment_meta = wp_get_attachment(your_attachment_id);.

Custom field/meta populated by dropdown of existing posts

Posted by Prem Tiwari | Updated on

I am a bit bias, but you essentially have a good working model outlined to follow depending on what ever route you choose. However, if using WPAlchemy, you would basically do something like the following: