A class for create new pages in WordPress theme or plugin. C... open

A class for create new pages in WordPress theme or plugin. Code example

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators

This is a PHP class named Plugin_Install that has a private static method named create_pages()
that creates new pages for a WordPress plugin. The method retrieves an array of page data from the new_pages filter and iterates through each item in the array. Each item represents a WordPress page that will be created if it doesn’t already exist.

The method also checks whether the page exists by calling the isset_plugin_page() function. If the page doesn’t exist, the method calls the create_plugin_page_if_need() function to create the page with the given title, content, and name. Finally, each created page’s data is stored in the $site_options array.

class Plugin_Install {

	private static function create_pages() {
		//we will write each new page in a variable so as not to recreate the pages. Can use wordress options
		global $site_options;

		$pages = apply_filters( 'new_pages', array(
			'account' => array(
				'name' => 'account',
				'title' => __( 'Personal cabinet', 'text-domain' ),
				'content' => 'Some content'
			),
			'Feed' => array(
				'name' => 'user-feed',
				'title' => __( 'FEED', 'text-domain' ),
				'content' => 'Some content'
			),
			'all_users' => array(
				'name' => 'users',
				'title' => __( 'Users', 'wp-recall' ),
				'content' => 'Some content'
			),
		) );

		foreach ( $pages as $key => $page ) {

			if ( is_array( $page ) ) {

				if ( ! isset_plugin_page( $key ) ) {
					//write new option
					$site_options[ $key ] = create_plugin_page_if_need( $key, [ 
						'post_title' => $page['title'],
						'post_content' => $page['content'],
						'post_name' => $page['name'],
					] );
				}

			}
		}
	}
}

function create_plugin_page_if_need( $page_id, $args ) {
	if ( ! isset_plugin_page( $page_id ) ) {
		return create_plugin_page( $page_id, $args );
	}

	return false;
}

function isset_plugin_page( $page_id ) {
	return get_plugin_page( $page_id ) ? true : false;
}

function get_plugin_page( $page_id ) {

	$plugin_pages = get_site_option( 'plugin_pages' );

	if ( ! isset( $plugin_pages[ $page_id ] ) ) {
		return false;
	}

	global $wpdb;
	$table  = $wpdb->prefix . 'posts';
	$result = $wpdb->get_var( "SELECT ID FROM {$table} WHERE ID = {$plugin_pages[ $page_id ]} AND post_status = 'publish'" );

	return $result;
}

function create_plugin_page( $page_id, $args ) {
	global $user_ID;

	$ID = wp_insert_post( wp_parse_args( $args, array(
		'post_status' => 'publish',
		'post_author' => $user_ID,
		'post_type' => 'page'
	) ) );

	if ( ! $ID ) {
		return false;
	}

	$plugin_pages = get_site_option( 'plugin_pages' );

	$plugin_pages[ $page_id ] = $ID;

	update_site_option( 'plugin_pages', $plugin_pages );

	return $ID;
}

Plugin_Install::create_pages();
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation