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

Using posts and postmeta table to store custom Address Book Plugin data

Using posts & postmeta: There is nothing wrong in using posts and postmeta table for custom database requirements. In WordPress, although posts & postmeta table was originally there to create blog posts, later it evolved to cover many different sorts of database requirements. Using Custom Post Types, Post Meta & Custom Taxonomies, you can satisfy … 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