Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress Plugin to create a React Public page on activation

I'm building a WordPress plugin that creates an admin menu and stores the value in the WordPress backend and then shows this stored value on a Public page. But the code is not creating a Public page where I want to load a custom react page.

In the root directory, I have wp-react-kickoff.php file like this

<?php
/**
 * Plugin Name: Batmobile Design
 * Author: Batman
 * Author URI: 
 * Version: 1.0.0
 * Description: WordPress React KickOff.
 * Text-Domain: wp-react-kickoff
 */

if( ! defined( 'ABSPATH' ) ) : exit(); endif; // No direct access allowed.

/**
* Define Plugins Contants
*/
define ( 'WPRK_PATH', trailingslashit( plugin_dir_path( __FILE__ ) ) );
define ( 'WPRK_URL', trailingslashit( plugins_url( '/', __FILE__ ) ) );

/**
 * Loading Necessary Scripts
 */
add_action( 'admin_enqueue_scripts', 'sd_scripts' );
function sd_scripts() {
    wp_enqueue_script( 'wp-react-kickoff', WPRK_URL . 'dist/bundle.js', [ 'jquery', 'wp-element' ], wp_rand(), true );
    wp_localize_script( 'wp-react-kickoff', 'appLocalizer', [
        'apiUrl' => home_url( '/wp-json' ),
        'nonce' => wp_create_nonce( 'wp_rest' ),
    ] );
}

require_once WPRK_PATH . 'classes/class-create-admin-menu.php';
require_once WPRK_PATH . 'classes/class-create-settings-routes.php';
require_once WPRK_PATH . 'classes/class-public-page.php';

Then I have created a folder called classes where I have

class-create-admin-menu.php, 
class-create-settings-routes.php, 
class-public-page.php, 
wprk-public-page-template.php.

The code specific to create Public page looks like this

<?php
/**
 * This file will create Public Page
 */


function wprk_create_public_page() {
    $page = [
        'post_type' => 'Automatic page',
        'post_title' => 'WP React KickOff Public Page',
        'post_content' => 'asasasas',
        'post_status' => 'publish',
        'post_author' => 1,
    ];
    $page_id = wp_insert_post( $page );
    update_post_meta( $page_id, '_wp_page_template', 'wprk-public-page-template.php' );
}

register_activation_hook( __FILE__, 'wprk_create_public_page' );

And my wprk-public-page-template.php

<div id="wprk-public-app"></div>
<?php
wp_enqueue_script( 'wp-react-Kickoff', WPRK_URL . 'dist/bundle.js', [], wp_rand(), true );
wp_localize_script( 'wp-react-Kickoff', 'appLocalizer', [
        'apiUrl' => home_url( '/wp-json' ),
        'nonce' => wp_create_nonce( 'wp_rest' ),
    ] );

And In the ReactFolder, I have App.js, both Settings (This is for admin menu, it is working properly) and Public Page rendered like this

import React from 'react';
import PublicPage from './components/PublicPage';
import Settings from './components/Settings';

function App() {
    return(
        <React.Fragment>
            <Settings />
            <PublicPage />
        </React.Fragment>
    )
}
export default App;

And for testing, let the Public page, look like this

import axios from 'axios';

const apiUrl = appLocalizer.apiUrl;
const nonce = appLocalizer.nonce;



import React from 'react';

function PublicPage(props) {
    return (
        <div>
            <h1>hello world asasa</h1>
        </div>
    );
}

export default PublicPage;

I'm quite new to programming. Could someone help me identify why the Public page is not getting created?

Please let me know if you need additional information to troubleshoot?

like image 507
Newbie_developer Avatar asked Feb 21 '26 10:02

Newbie_developer


1 Answers

your post_type argument in your create page function isnt valid.

If you have a custom post type you'll want to use the slug, otherwise, in your case if i understand correctly you want to create a standard wordpress page which the post type is page.

function wprk_create_public_page() {
    $page = [
        'post_type' => 'page',
        'post_title' => 'WP React KickOff Public Page',
        'post_content' => 'asasasas',
        'post_status' => 'publish',
        'post_author' => 1,
    ];
    $page_id = wp_insert_post( $page );
    update_post_meta( $page_id, '_wp_page_template', 'wprk-public-page-template.php' );
}

register_activation_hook( __FILE__, 'wprk_create_public_page' );
like image 175
Moishy Avatar answered Feb 23 '26 01:02

Moishy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!