1

I am trying to develop my first Wordpress plugin using OOP by following a guide. I have followed word for word so far yet I am struggling with an issue that I am unable to rewrite what I imagine will be the URL.

This is the code I am using...

class BookItNow
{
function __construct()
{
    add_action('init', $this->custom_post_type());
}

function activate()
{
    $this->custom_post_type();
    flush_rewrite_rules();
}

function register()
{
    add_action('admin_enqueue_scripts', array($this, 'enqueue'));
}

function deactivate()
{
    flush_rewrite_rules();
}

function custom_post_type()
{
    register_post_type('bookings', ['public' => true, 'label' => 'Bookings']);
}

function enqueue()
{
    wp_enqueue_style('style', plugins_url('/assets/style.css', __FILE__));
}
}

if (class_exists('BookItNow')) {
    $book = new BookItNow();
    $book->register();
}

register_activation_hook(__FILE__, array($book, 'activate'));
register_deactivation_hook(__FILE__, array($book, 'deactivate'));
register_uninstall_hook(__FILE__, array($book, 'uninstall'));

And this is the error I am getting Uncaught Error: Call to a member function add_rewrite_tag() on null. Removing the $this->custom_post_type() removes this issue, so I have no idea. Any help would be appreciated.

  • 1
    You should indent your class methods by 1 so it's clear which code is a part of the class and which code is not a part of the class. Consider looking up the WP Coding standards, it will make your code easier to read/write and share, and prevent certain types of mistakes – Tom J Nowell Jan 7 '18 at 21:09
4

try these changes:

1) To use add_action() when your plugin or theme is built using classes instead of passing just the function name, you need to pass an array with a reference to the class ($this) and the callable function ('custom_post_type')

function __construct()
{
    add_action('init', array( $this, 'custom_post_type' ) );
}

2) The original 'activate' method called 'custom_post_type' suggesting that it only needs to be called once when the plugin is activated. But to properly register the custom post type, this method needs to be called every time Wordpress is loaded which is why it is added to the 'init' hook. Calling it in the activate method doesn't really make sense so I removed it.

function activate()
{
    flush_rewrite_rules();
}
  • This doesn't explain why it fixes the issue, or what the original problem was, can you edit the answer to explain what the OP did wrong so that they can learn? – Tom J Nowell Jan 7 '18 at 21:11
  • sure no problem, i've updated my original answer – locomo Jan 8 '18 at 23:49
  • Sorry I've only just got round to accepting this as an action. Thank you, it did the trick! I have however since rewrote the way my plugin works. Hopefully people struggling with the same problem will get the answer here :) – tallent123 Jan 9 '18 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.