customization - Issue calling my functions on Plugin activate in Wordpress

I am Creating New Wordpress Plugin and splitting my code into different files for code management. Here is code of my m

I am Creating New Wordpress Plugin and splitting my code into different files for code management. Here is code of my main Plugin file which my-best-plugin.phpwith following code:

<?php
    if ( ! defined('ABSPATH') ){
        die;
    }

    if(!defined('MY_PLUGIN_PATH')) { 
        define( 'MY_PLUGIN_PATH', plugin_dir_path(__FILE__) ); 
    }

    if( ! class_exists('MyBestPlugin') ){
        class MyBestPlugin {

            function mbp_activate(){
                require_once MY_PLUGIN_PATH .'inc/mbp-plugin-activate.php';
                MBPActivate::activate();
            }

        }

        $pluginInstance = new MyBestPlugin();

        // activation
        register_activation_hook( __FILE__ , array( $pluginInstance, 'mbp_activate' ) );

    }

Now in my second file which is for activation is located in inc/mbp-plugin-activate.php and code is below:

<?php

    class MBPActivate {

        public static function activate(){
            // I want to do more here by calling functions
            // cpt(); 
            // dont know how to call function cpt()
            // also not sure is it running properly or not :P
            add_action('init', array( 'MBPActivate','cpt' ));
            flush_rewrite_rules();
        }

        public static function cpt(){
            register_post_type('book', ['public'=>true, 'label'=>'Books']);
        }

    }

first Im not sure my plugin activate file is running or not but giving No error, also when I call my own functions it gives fatal error OR headers already sent error. Please tell me how to call my functions during activation from activate plugin file. Thanks in advance :)

I am Creating New Wordpress Plugin and splitting my code into different files for code management. Here is code of my main Plugin file which my-best-plugin.phpwith following code:

<?php
    if ( ! defined('ABSPATH') ){
        die;
    }

    if(!defined('MY_PLUGIN_PATH')) { 
        define( 'MY_PLUGIN_PATH', plugin_dir_path(__FILE__) ); 
    }

    if( ! class_exists('MyBestPlugin') ){
        class MyBestPlugin {

            function mbp_activate(){
                require_once MY_PLUGIN_PATH .'inc/mbp-plugin-activate.php';
                MBPActivate::activate();
            }

        }

        $pluginInstance = new MyBestPlugin();

        // activation
        register_activation_hook( __FILE__ , array( $pluginInstance, 'mbp_activate' ) );

    }

Now in my second file which is for activation is located in inc/mbp-plugin-activate.php and code is below:

<?php

    class MBPActivate {

        public static function activate(){
            // I want to do more here by calling functions
            // cpt(); 
            // dont know how to call function cpt()
            // also not sure is it running properly or not :P
            add_action('init', array( 'MBPActivate','cpt' ));
            flush_rewrite_rules();
        }

        public static function cpt(){
            register_post_type('book', ['public'=>true, 'label'=>'Books']);
        }

    }

first Im not sure my plugin activate file is running or not but giving No error, also when I call my own functions it gives fatal error OR headers already sent error. Please tell me how to call my functions during activation from activate plugin file. Thanks in advance :)

Share Improve this question edited Jun 26, 2019 at 11:31 Faisal Janjua asked Jun 26, 2019 at 11:10 Faisal JanjuaFaisal Janjua 1012 bronze badges 8
  • You're going to need to give a specific example of something that's not working. As far as I can tell, what's in this question is fine. – Jacob Peattie Commented Jun 26, 2019 at 11:22
  • now look at the code of activate file. this is how Im declaring new Custom Post Type which is not showing in menu. – Faisal Janjua Commented Jun 26, 2019 at 11:34
  • Custom post types aren't registered on activation. They're supposed to be registered on the init hook. Only functions that write to the database or files are typically run on activation. Most functions run on a hook of some kind. – Jacob Peattie Commented Jun 26, 2019 at 11:37
  • Yes Im getting your point, but can you please tell me how to call some other custom function on activate? like I can create table in DB via that function etc – Faisal Janjua Commented Jun 26, 2019 at 11:42
  • 1 Just run the function inside the activate function. There's nothing more to it. You're doing the right thing to run functions on activation, you're just using the wrong functions. – Jacob Peattie Commented Jun 26, 2019 at 11:45
 |  Show 3 more comments

2 Answers 2

Reset to default 0

Please try below code

my-plugin.php file

<?php
/**
Plugin Name: My Plugin
Plugin URI: http://#####
Description: This plugin just for test
Author: Chetan Vaghela
Version: 1.0.0
Author URI: http://che##la.###/
*/
if ( ! defined('ABSPATH') ){
        die;
}
if(!defined('MY_PLUGIN_PATH')) { 
    define( 'MY_PLUGIN_PATH', plugin_dir_path(__FILE__) ); 
}

function mbp_activate(){
  flush_rewrite_rules();
  add_option( 'my_plugin_activated', time() );
}
require_once MY_PLUGIN_PATH .'inc/mbp-plugin-activate.php';
function run_mbpactive() {

    $plugin = new MBPActivate();

}
run_mbpactive();

register_activation_hook( __FILE__ , 'mbp_activate'  );


register_uninstall_hook( __FILE__, 'my_plugin_uninstall' );
function my_plugin_uninstall() {
     unregister_post_type( 'book' );
     delete_option("my_plugin_activated");
}

inc/mbp-plugin-active.php file

<?php
class MBPActivate {

     public function __construct() {

        add_action( 'init', array($this, 'cptcallback' ) );

    }

    public function cptcallback(){

       // / die("calledddd");
        $books_labels = array(
            'name'               => _x( 'Books', 'Books name', 'text-domain' ),
            'singular_name'      => _x( 'Book', 'Books name', 'text-domain' ),
            'menu_name'          => _x( 'Books', 'admin menu', 'text-domain' ),
            'name_admin_bar'     => _x( 'Books', 'add new on admin bar', 'text-domain' ),
            'add_new'            => _x( 'Add New', 'Books', 'text-domain' ),
            'add_new_item'       => __( 'Add New Books', 'text-domain' ),
            'new_item'           => __( 'New Books', 'text-domain' ),
            'edit_item'          => __( 'Edit Books', 'text-domain' ),
            'view_item'          => __( 'View Books', 'text-domain' ),
            'all_items'          => __( 'All Books', 'text-domain' ),
            'search_items'       => __( 'Search Books', 'text-domain' ),
            'parent_item_colon'  => __( 'Parent Books:', 'text-domain' ),
            'not_found'          => __( 'No Books found.', 'text-domain' ),
            'not_found_in_trash' => __( 'No Books found in Trash.', 'text-domain' )
        );

        $books_args = array(
            'labels'             => $books_labels,
            'description'        => __( 'Description.', 'text-domain' ),
            'public'             => true,
            'publicly_queryable' => true,
            'show_ui'            => true,
            'show_in_menu'       => true,
            'query_var'          => true,
            'rewrite'            => array( 'slug' => 'book' ),
            'capability_type'    => 'post',
            'has_archive'        => true,
            'hierarchical'       => false,
            'menu_position'      => null,
            'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
            'menu_icon'          => 'dashicons-testimonial'
        );
        register_post_type( 'book', $books_args );

    }
}

let me know if it is works for you!

I do something like this:-

   // Make sure we don't expose any info if called directly
if ( ! defined('ABSPATH') ){
    die;
}

if(!defined('MY_PLUGIN_PATH')) { 
    define( 'MY_PLUGIN_PATH', plugin_dir_path(__FILE__) ); 
}

if( ! class_exists('MyBestPlugin') ){
        class MyBestPlugin {

            public function __construct(){
                register_activation_hook( __FILE__ , array( $this, 'mbp_activate' ) );
                register_deactivation_hook( __FILE__ , array( $this, 'mbp_deactivate' ) );
                if(get_option('cpt_enable')){
                   add_action( 'init', array($this, 'cpt' ) ); 
                } //get the 'cpt enabled option if available.Hit the hook'
            }

            public function mbp_activate(){
                require_once MY_PLUGIN_PATH .'inc/yes.php';
                MBPActivate::activate();
            }

            public function mbp_deactivate(){
                require_once MY_PLUGIN_PATH .'inc/yes.php';
                MBPActivate::deactivate();
            }

            public function cpt(){
                $args = array(
                                'public'    => true,
                                'label'     => __( 'Books', 'textdomain' ),
                                'menu_icon' => 'dashicons-book',
                            );
                register_post_type( 'book', $args );
            }

        }

        $pluginInstance = new MyBestPlugin();

}

//////next page yes.php////

class MBPActivate {
        public function activate(){
            add_option('cpt_enable','cpt_enable'); // set the cpt enabled option after activation hook.
            flush_rewrite_rules();
        }
        public function deactivate(){
            delete_option('cpt_enable');
            flush_rewrite_rules();
        }
    }
    $plugin = new MBPActivate();

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

相关推荐

  • customization - Issue calling my functions on Plugin activate in Wordpress

    I am Creating New Wordpress Plugin and splitting my code into different files for code management. Here is code of my m

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信