Custom Post Type works when registered in functions.php but not when registered in plugin

I am creating a plugin which registers a custom post type called 'news articles' see the class for this code b

I am creating a plugin which registers a custom post type called 'news articles' see the class for this code below:

<?php
/**
 * This plugin class is used to register the necessary custom post types
*/
class Dw_Mf_Register_CPT {

/**
 * Register Custom Post Types
 *
 * @since 1.0.0
 * @access public
 * @return void
 */
public function register_cpt(){
    add_action( 'init', array( $this, 'create_news_article_cpt') );
}

/**
 * Add News Article Custom Post Type
 *
 * @since 1.0.0
 * @access public
 * @return void
 */
public function create_news_article_cpt(){

    $supports = array(
        'title', // post title
        'editor', // post content
        'author', // post author
        'thumbnail', // featured images
        'excerpt', // post excerpt
        'custom-fields', // custom fields
        'comments', // post comments
        'revisions', // post revisions
        'post-formats', // post formats
        );
        $labels = array(
        'name' => _x('news articles', 'plural'),
        'singular_name' => _x('news article', 'singular'),
        );
        $args = array(
        'supports' => $supports,
        'labels' => $labels,
        'public' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'news-articles'),
        'has_archive' => true,
        'hierarchical' => false,
        );
        register_post_type('news articles', $args);
}
}

It works on the back end. I am able to see the custom post type in the menu and I am able to create new news articles.

However, when I try to view any of these news articles I am 301 redirected to the homepage.

When I take the same code and remove it from a PHP class context and put it directly into functions.php as a standard php function and register the custom post type it works as expected and I am able to view the news articles I create on the front of the website without them redirecting to the homepage.

//functions.php code
function create_post_type_news() {
$supports = array(
    'title', // post title
    'editor', // post content
    'author', // post author
    'thumbnail', // featured images
    'excerpt', // post excerpt
    'custom-fields', // custom fields
    'comments', // post comments
    'revisions', // post revisions
    'post-formats', // post formats
    );
    $labels = array(
    'name' => _x('news articles', 'plural'),
    'singular_name' => _x('news article', 'singular'),
    );
    $args = array(
    'supports' => $supports,
    'labels' => $labels,
    'public' => true,
    'query_var' => true,
    'rewrite' => array('slug' => 'news-articles'),
    'has_archive' => true,
    'hierarchical' => false,
    );
    register_post_type('news articles', $args);
}
add_action('init', 'create_post_type_news');

Ideally, this would work in a plugin and I wouldn't need to register the CPT in the functions.php file. Any insight on what the problem might be?

I am creating a plugin which registers a custom post type called 'news articles' see the class for this code below:

<?php
/**
 * This plugin class is used to register the necessary custom post types
*/
class Dw_Mf_Register_CPT {

/**
 * Register Custom Post Types
 *
 * @since 1.0.0
 * @access public
 * @return void
 */
public function register_cpt(){
    add_action( 'init', array( $this, 'create_news_article_cpt') );
}

/**
 * Add News Article Custom Post Type
 *
 * @since 1.0.0
 * @access public
 * @return void
 */
public function create_news_article_cpt(){

    $supports = array(
        'title', // post title
        'editor', // post content
        'author', // post author
        'thumbnail', // featured images
        'excerpt', // post excerpt
        'custom-fields', // custom fields
        'comments', // post comments
        'revisions', // post revisions
        'post-formats', // post formats
        );
        $labels = array(
        'name' => _x('news articles', 'plural'),
        'singular_name' => _x('news article', 'singular'),
        );
        $args = array(
        'supports' => $supports,
        'labels' => $labels,
        'public' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'news-articles'),
        'has_archive' => true,
        'hierarchical' => false,
        );
        register_post_type('news articles', $args);
}
}

It works on the back end. I am able to see the custom post type in the menu and I am able to create new news articles.

However, when I try to view any of these news articles I am 301 redirected to the homepage.

When I take the same code and remove it from a PHP class context and put it directly into functions.php as a standard php function and register the custom post type it works as expected and I am able to view the news articles I create on the front of the website without them redirecting to the homepage.

//functions.php code
function create_post_type_news() {
$supports = array(
    'title', // post title
    'editor', // post content
    'author', // post author
    'thumbnail', // featured images
    'excerpt', // post excerpt
    'custom-fields', // custom fields
    'comments', // post comments
    'revisions', // post revisions
    'post-formats', // post formats
    );
    $labels = array(
    'name' => _x('news articles', 'plural'),
    'singular_name' => _x('news article', 'singular'),
    );
    $args = array(
    'supports' => $supports,
    'labels' => $labels,
    'public' => true,
    'query_var' => true,
    'rewrite' => array('slug' => 'news-articles'),
    'has_archive' => true,
    'hierarchical' => false,
    );
    register_post_type('news articles', $args);
}
add_action('init', 'create_post_type_news');

Ideally, this would work in a plugin and I wouldn't need to register the CPT in the functions.php file. Any insight on what the problem might be?

Share Improve this question asked Aug 7, 2019 at 11:28 dan webbdan webb 1012 bronze badges 1
  • You have space in custom post type slug: register_post_type('news articles', $args);. – nmr Commented Aug 7, 2019 at 11:47
Add a comment  | 

1 Answer 1

Reset to default 0
  1. Your class has no __construct() method.
  2. Even if it did, you're not calling the class to set it up

So...

1) at the top of your class, add:

function __construct() {
    register_cpt();
}

2) after your class, add:

$myCPT = new Dw_Mf_Register_CPT();

Or, you could just get rid of the class construction altogether. You don't actually need it if all your plugin does is add the CPT.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745262222a4619262.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信