How to register custom post types in a plugin?

The init hook is the first hook allowed. If called earlier it won’t work. See WP Codex: https://developer.wordpress.org/plugins/post-types/registering-custom-post-types/ Create or modify a post type. register_post_type should only be invoked through the ‘init’ action. It will not work if called before ‘init’, and aspects of the newly created or modified post type will work incorrectly if … Read more

Options page not displayed under Settings menu

The only issue was that the capability was specified incorrectly when using add_options_page(). The capability should be manage_options. Note the underscore, no space: function basic_treehouse_badges_menu() { /* * Use the add_options_page function * add_options_page( $page_title, $menu_title, $capability, $menu-slug, $function ) * */ add_options_page( ‘Official Tree House Badges Plugin’, ‘Treehouse Badges’, ‘manage_options’, ‘wp-treehouse-badges’, ‘wptreehouse_badges_option_page’ ); } … Read more

Checking for user role in a custom plugin

Why it doesn’t work for a Plugin: It doesn’t work for plugin because by the time WordPress executes $current_user = wp_get_current_user(); in a plugin, the function wp_get_current_user() isn’t defined yet. WordPress loads that plugin file before it defines those user related functions. However, it works for theme because WordPress loads theme after it defines and … Read more

WordPress hook for visiting a post

I assume you are looking for a conditional to check if you are on a post page. You can do so by using is_single(): if (is_single()) { // Update your post views here } If you insist on using filters or hooks, you can use the_content filter: add_filter( ‘the_content’, ‘update_post_views’ ); function update_post_views($content){ if (is_single()) … Read more

Upload images from custom plugin using the media modal

Ah! The classic issue anyone that’s cared about their users’ experience has come to face. As per my experience, wp.media is the way to go. This is not my code, but it gets the job done. I’ve used it plenty. I’ll explain what it does, piece by piece: // Source: https://vedmant.com/using-wordpress-media-library-in-widgets-options/ jQuery(document).ready(function ($) { $(document).on(“click”, … Read more