0

I am little bit confuse. I have created a custom plugin and added a functionality to create a page automatically on activating the plugin. But i want to add form fields that is so much bigger form so i have created another file with name resume-form.php and i want to include this file at the $page['post_content']= ''; as i am new in plugin development please help me out -

function create_page(){
    $page = array();
    $page['post_title']= "Resume Builder Default Page";
    $page['post_content']= '';
    $page['post_status']= "publish";
    $page['post_slug'] = "resume-builder-default-plugin-page";
    $page['post_type'] = "page";
    $post_id = wp_insert_post($page);
    add_option("custom_plugin_page_id",$post_id);
}
register_activation_hook(__FILE__,"create_page");

1 Answer 1

2

I would approach this in different way.

In this original index.php file, keep it simple, like this:

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

if ( ! class_exists( 'My_Plugin' ) ) {
    require_once __DIR__ . '/includes/class-my-plugin.php';
    $my_plugin = My_Plugin::get_instance();
}

In this new file you create class:

class My_Plugin {

    protected static $instance = null;

    protected $basedir = null;
    

    public static function get_instance() {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    public function get_basedir() {
        return $this->basedir;
    }

    protected function __construct() { 
        $this->basedir = dirname( dirname( __FILE__ ) );

        // Here put your hooks
    }
}

With this basic construct, you can use hooks to create your plugin features.

If you want to make page for your plugin features, do not create post with post type page, but create config page using add_menu_page

https://developer.wordpress.org/reference/functions/add_menu_page/

It will be visible in menu on the left in wp-admin.

It can be done like this:

add_action( 'admin_menu', array( $this, 'register_settings_page' ) );

This code put in __constuct, admin_menu is a hook name, and array( $this, 'register_settings_page' ) points that function that should be called is inside this class file, and function name is "register_settings_page"

And create function like this:

public function register_settings_page() {
        add_menu_page(
            'My Plugin',
            'My Plugin',
            'manage_options',
            'surfer',
            array( $this, 'settings_page' )
        );
    }

Description of all params you can find in documentation: https://developer.wordpress.org/reference/functions/add_menu_page/

And again you create function "settings_page" where you can build content:

Like this:

public function settings_page() {
    require_once My_Plugin::get_instance()->get_basedir() . '/templates/settings.php';
}

With this build, you should have this structure:

- index.php
- includes
- - class-my-plugin.php
- templates
- - settings.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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