0

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 :)

  • 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 Jun 26 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 Jun 26 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 Jun 26 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 Jun 26 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 Jun 26 at 11:45
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!

0

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();
  • how can I run my custom functions on activating plugin? – Faisal Janjua Jun 26 at 12:24
  • Updated and checked. it's working fine. – Techno Deviser Jun 29 at 3:38

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.