How to add a page to a wordpress site and give it a specific shortcode via a custom plugin


davidb3rn

I've made a move that allows pages to be created if the switch in the settings is checked. After confirming, it creates the page but doesn't add the template. How can I get my template to work properly?

        add_action( 'admin_init', 'cart_page' );
    function cart_page() {
    //Add cart page to website
    
        if ( get_option( 'cart' ) === "checked" AND get_option('cart_exist') === false) {
    //        IF CART HAS BEEN CHECKED
            $new_page_id = wp_insert_post( array(
                'post_title'     => 'Cart',
                'post_type'      => 'page',
                'post_name'      => 'Cart',
                'comment_status' => 'closed',
                'ping_status'    => 'closed',
                'post_content'   => '',
                'post_status'    => 'publish',
                'post_author'    => get_user_by( 'id', 1 )->user_id,
                'menu_order'     => 0,
                // Assign page template
                'page_template'  => plugins_url('page_templates/cart_template.php', __FILE__ )
    
    
            ) );
            wp_insert_post($new_page_id);
            update_option( 'cart_exist', true );
        }
    
        else if (get_option('cart') === "" AND get_option('cart_exist') === true) {
    //        IF CUSTOMER DOES NOT WANT CART
            update_option( 'cart_exist', false );
        }
    }

This is how my template page looks in the plugin.

get_header();

echo do_shortcode( '[cart_portal]' );

get_footer();
?>

davidb3rn

In my case, I have found a solution to this simple problem. If I just need to add a shortcode, I can add it to "post_content". I'm still wondering how to add templates. However, you can still use a file to import the entire layout. But it doesn't work with visual composer.

See example below...

 $new_page_id = array(
            'post_title'     => 'Cart',
            'post_type'      => 'page',
            'post_name'      => 'Cart',
            'comment_status' => 'closed',
            'ping_status'    => 'closed',
            'post_content'   => '[cart_portal]',
            'post_status'    => 'publish',
            'post_author'    => get_user_by( 'id', 1 )->user_id,
            'menu_order'     => 0,

        );
        wp_insert_post($new_page_id);

Related


Add custom text to contact form via shortcode WordPress 7

Cray I'm using Contact Fom 7 and need to add custom text somewhere on the form. I am using PHP to get custom text from a subfield of an advanced custom field. I know, there is an extra plugin called "Contact Form 7 Dynamic Text Extension" ( https://de.wordpres

Add custom text to contact form via shortcode WordPress 7

Cray I'm using Contact Fom 7 and need to add custom text somewhere on the form. I am using PHP to get custom text from a subfield of an advanced custom field. I know, there is an extra plugin called "Contact Form 7 Dynamic Text Extension" ( https://de.wordpres

WordPress add custom field to page without plugin

Master Weems I am new to WordPress and am building a website using a custom template. The template includes a large banner at the top that can be uploaded via the page's "featured image". I would like to add the page title on top of this image (this is not a p

WordPress add custom field to page without plugin

Master Weems I am new to WordPress and am building a website using a custom template. The template includes a large banner at the top that can be uploaded via the page's "featured image". I would like to add the page title on top of this image (this is not a p

WordPress add custom field to page without plugin

Master Weems I am new to WordPress and am building a website using a custom template. The template includes a large banner at the top that can be uploaded via the page's "featured image". I would like to add the page title on top of this image (this is not a p

WordPress add custom field to page without plugin

Master Weems I am new to WordPress and am building a website using a custom template. The template includes a large banner at the top that can be uploaded via the page's "featured image". I would like to add the page title on top of this image (this is not a p

How to Add Custom Shortcode Buttons in TinyMCE Editor WordPress

Harun R Rayhan Hi, I am using the latest version of WordPress. I want to add my shortcode in TinyMCE editor like this image : http://prntscr.com/72ycrs My shortcode is: [my_tabs] [my_tab title = "Tab One Title"]Tab One Content Goes Here[/my_tab] [my_tab title

How to Add Custom Shortcode Buttons in TinyMCE Editor WordPress

Harun R Rayhan Hi, I am using the latest version of WordPress. I want to add my shortcode in TinyMCE editor like this image : http://prntscr.com/72ycrs My shortcode is: [my_tabs] [my_tab title = "Tab One Title"]Tab One Content Goes Here[/my_tab] [my_tab title

How to Add Custom Shortcode Buttons in TinyMCE Editor WordPress

Harun R Rayhan Hi, I am using the latest version of WordPress. I want to add my shortcode in TinyMCE editor with an image like this : http://prntscr.com/72ycrs My shortcode is: [my_tabs] [my_tab title = "Tab One Title"]Tab One Content Goes Here[/my_tab] [my_ta

How to Add Custom Shortcode Buttons in TinyMCE Editor WordPress

Harun R Rayhan Hi, I am using the latest version of WordPress. I want to add my shortcode in TinyMCE editor with an image like this : http://prntscr.com/72ycrs My shortcode is: [my_tabs] [my_tab title = "Tab One Title"]Tab One Content Goes Here[/my_tab] [my_ta

Add css js to plugin wordpress that calls shortcode

Christophe Lacroix I have to make a wp plugin that calls a page with a shortcode: function shortcode_flexSlide(){ include ('eleve.php'); } add_shortcode('flexSlide', 'shortcode_flexSlide'); Now i have to load css and js to link to page 'eleve.ph

Add css js to plugin wordpress that calls shortcode

Christophe Lacroix I have to make a wp plugin that calls a page with a shortcode: function shortcode_flexSlide(){ include ('eleve.php'); } add_shortcode('flexSlide', 'shortcode_flexSlide'); Now i have to load css and js to link to page 'eleve.ph

How to enable additional page in WordPress custom plugin?

user1736479 I have a custom WordPress plugin that is showing me a list of data from database. I am registering it's page via: add_menu_page( 'Naročila', 'Vsa naročila', 'administrator', 'listaj-narocila', array( &$this, 'listaj_narocil

How to call image via WordPress shortcode attribute?

Shahryal Kabir I'm developing a WordPress plugin for wordpress.org and this is my first plugin. I can't solve my own problem (I googled a lot and still can't find any solution). But maybe for some people, the solution to this problem is simple. So if you know

How to call image via WordPress shortcode attribute?

Shahryal Kabir I'm developing a WordPress plugin for wordpress.org and this is my first plugin. I can't solve my own problem (I googled a lot and still can't find any solution). But maybe for some people, the solution to this problem is simple. So if you know

How to add a custom menu to a specific location in Wordpress

Mayanke I want to add a custom menu in the main menu, for this I have used this menu in the code below, add_filter( 'wp_nav_menu_items', 'search_menu_item', 10, 2 ); function search_menu_item ( $items, $args ) { if ($args->theme_location == 'secondary-menu') {

How to add a custom menu to a specific location in Wordpress

Mayanke I want to add a custom menu in the main menu, for this I have used this menu in the code below, add_filter( 'wp_nav_menu_items', 'search_menu_item', 10, 2 ); function search_menu_item ( $items, $args ) { if ($args->theme_location == 'secondary-menu') {

How to add a custom menu to a specific location in Wordpress

Mayanke I want to add a custom menu in the main menu, for this I have used this menu in the code below, add_filter( 'wp_nav_menu_items', 'search_menu_item', 10, 2 ); function search_menu_item ( $items, $args ) { if ($args->theme_location == 'secondary-menu') {

How to add video in WordPress custom page template?

Ashiquzzaman Kiron For Posts, you can take advantage of the embed option provided by WordPress, which is easy. But for custom page templates, is there an easy solution? So far I have found this - https://codex.wordpress.org/Function_Reference/wp_oembed_add_pro

How to add custom data in post page? WordPress

Goraz <form method="post"> <input type="text" name="title" /> //Title <input type="text" name="desc" /> //Description <input type="text" name="age" /> //Age autor </form> This code adds a title and description to the new article. But how do I add custom dates

How to add a post to a custom page in WordPress:

Raj Damani I am new to WordPress. Suppose I do the following on the homepage, news and about us pages Now if I just want to create news for news pages, news1, news2 Then post a new post, how can I set this? Vasso 123 You can create page templates . Create one