Create a page with gutenberg HTML block automatically upon plugin activation - php

I have managed to create a page upon plugin activation.
i need to add content to the page that get created. so i used following code. The problem is it creates a classic editor in the page and not the gutenberg HTML editor.
register_activation_hook( __FILE__, 'my_plugin_install_function');
function my_plugin_install_function()
{
//post status and options
$post = array(
'comment_status' => 'closed',
'ping_status' => 'closed' ,
'post_author' => get_current_user_id(),
'post_date' => date('Y-m-d H:i:s'),
'post_name' => 'Checklists',
'post_status' => 'publish' ,
'post_content' => '[customization-shortcode]',
'post_title' => 'Checklists',
'post_type' => 'dash',
);
//insert page and save the id
$newvalue = wp_insert_post( $post, false );
//save the id in the database
update_option( 'hclpage', $newvalue );
}
i expect to have my shortcode displayed in a gutenberg HTML block and not the classic editor

I have found a solution to this, all i did is change following line:
'post_content' => '[customization-shortcode]',
to
'post_content' => '<!-- wp:html -->[customization-shortcode]<!-- /wp:html -->',
This will create a usual gutenberg HTML block instead the old classic one.

Related

Create custom post type whenever another custom post type is created

I'm trying to add a feature to my Wordpress website, that every time I add a post (custom post type), another post (different custom post type) is created.
I have tried adding an action in my function file but it does not seem to work.
functions.php
function add_notification($post_id) {
global $wpdb;
$post_type = get_post_type($post_id);
if($post_type == 'exercises'){
$title = "BLA";
$post_id_new = wp_insert_post(
array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_title' => $title,
'post_status' => 'publish',
'post_type' => 'notification'
)
);
}
}
add_action('publish_post', 'add_notification');
I have also tried it with other hooks like new_to_publish and publish_post
With that I was expecting to have a new post (notification) when I add an exercise post.
I think it's something silly I'm forgetting but it's been like 3 days that I'm stuck in this problem.
For custom post type we have to use hook like
add_action('publish_custom-post-name','function');
In your case you have to use Like Below
function add_notification($post_id) {
global $wpdb;
$post_type = get_post_type($post_id);
if($post_type == 'exercises'){
$title = "BLA";
$post_id_new = wp_insert_post(
array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_title' => $title,
'post_status' => 'publish',
'post_type' => 'notification'
)
);
}
}
add_action('publish_exercises', 'add_notification');
Please Try this. If any query feel free to ask.

Create a wordpress post with PHP

I have a problem, I can create a new post in my WordPress blog using PHP code:
$new_post = array(
'post_title' => $leadTitle,
'post_content' => $leadContent,
'post_status' => $postStatus,
'post_date' => $timeStamp,
'post_author' => $userID,
'post_type' => $postType,
'post_category' => array($categoryID),
'tags_input' => array($tags)
);
$post_id = wp_insert_post($new_post);
My problem is that whenever post_content has HTML code in it, it does not get included with the post.
How can I include iframe tags within my post_content?
Use the iframe shortcode instead.
[iframe src=”http://www.youtube.com/embed/4qsGTXLnmKs” width=”100%” height=”500″]
WordPress removes iframe html tags because of security reasons.
Iframe shortcode is the replacement of the iframe html tag and accepts the same params as iframe html tag does.

Is there a way to programmatically import wordpress pages within wordpress?

I'm trying to create this functionality within my wordpress plugin. Let's say I have a set number of pages that will never change and I want to automatically import them to every wordpress site I set up without having to manually go to the first site, export the xml file containing the pages then import it to the new site. Any thoughts on this?
Thanks
If you know how to loop through your XML file and your XML file is accessible on the other site you could loop through the following code:-
// Create post object
$my_post = array(
'post_title' => wp_strip_all_tags( $post_title ),
'post_content' => $post_content,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => $cat
);
// Insert the post into the database
wp_insert_post( $my_post );
You would need to initiate this code on the install of your plugin.
You can export your pages via default Wordpress export tool. That will result in .xml (WXR) file.
After that you can import the pages via WP-CLI tool on each one site, with the following command:
$ wp import file-name.xml
WXR stands for WordPress eXtended RSS.
You can store the pages in an array and then automatically insert them when your plugin is activated. I'd recommend storing a meta_key for each page that also let's you know it's already been inserted so that you don't create them every time the plugin is activated and deactivated. You can put this in the main file of your plugin. Be sure to replace the numbered pages and slugs with actual page names and to replace "my_plugin" with your plugin's namespace.
<?php
function create_my_plugin_pages() {
$pages = array(
'Page 1' => 'page-1', // Use slugs to create meta-keys
'Page 2' => 'page-2',
'Page 3' => 'page-3'
);
foreach( $pages as $title => $slug ) {
$meta_key = 'my-plugin_'.$slug;
// Check that the page wasn't already created
$existing = get_posts(array(
'post_type' => 'page',
'meta_query' => array(
array(
'key' => $meta_key,
'value' => '1'
)
)
));
// Create page if it doesn't exist
if ( !count($existing) ) {
$new_page = wp_insert_post(array(
'post_title' => $title,
'post_status' => 'publish'
));
add_post_meta($new_page,$meta_key,'1');
}
}
}
register_activation_hook( __FILE__, 'create_my_plugin_pages' );
?>

Wordpress Plugin - Delete a Page Created when deactivating

I tried to make a custom post type plugin for my own use, and managed to make a function that creates the page for it so far. What I want to do is to delete the said page when the plugin is activated. How should the code be?
This is my code for creating the said page upon plugin activation:
function create_video_pages() {
$post = array(
'comment_status' => 'open',
'ping_status' => 'closed' ,
'post_date' => date('Y-m-d H:i:s'),
'post_name' => 'videos',
'post_status' => 'publish' ,
'post_title' => 'Videos',
'post_type' => 'page',
);
$newvalue = wp_insert_post( $post, false );
update_option( 'vidpage', $newvalue );
}
Get the post_id from your vidpage option.
Then use it to delete that post.
function deactivate_plugin() {
$page_id = get_option('vidpage');
wp_delete_post($page_id);
}
register_deactivation_hook( __FILE__, 'deactivate_plugin' );
You can do this using register_deactivation_hook and function wp_delete_post which deletes post with everything that is tied to it.
What about this?
function on_deactivating_your_plugin() {
$page = get_page_by_path( 'about' );
wp_delete_post($page->ID);
}
register_deactivation_hook( __FILE__, 'on_deactivating_your_plugin' );

Add html/PHP pages on plugin activation (Wordpress)

Well am creating my first wordpress plugin, which should create a page (relatively large) on activation.
Currently i can create a file using :
$_p['post_title'] = $the_page_title;
$_p['post_content'] = '<h1>PAGE CONTENT</h1>';
$_p['post_status'] = 'publish';
$_p['post_type'] = 'page';
$_p['comment_status'] = 'closed';
$_p['ping_status'] = 'closed';
$_p['post_category'] = array(1); // the default 'Uncatrgorised'
// Insert the post into the database
$the_page_id = wp_insert_post( $_p );
The problem is, I cant put all the content of the file(which is large) as string to 'post_content' index. I want to know if there is a way, where i can simply either:
'post_content' => link to the file in my plugin directory
'post_content' => call a function which will return html content as string :( [worst case]
OR, Some more simpler way to achieve the objective.
Please help me.
Well, what i did to solve the problem is:
//**SECTION : 1**
function user_login_foo() {
return get_login_form(); // get_login_form() function will return the html template i want to display.
}
add_shortcode('user_login', 'user_login_foo'); // created a shortcode
**// SECTION: 2**
function get_login_form()
{
ob_start(); ?>
<h3><?php __('Login'); ?></h3>
<form action="" method="post">
<fieldset>
// the login form comes here
</fieldset>
</form>
<?php
return ob_get_clean();
}
function validate_login_user() {
// the login validation logic comes here
}
add_action('init', 'validate_login_user');
SECTION 1: registered a shortcode that will call a function [say,foo1()] and return the value.
The function foo1() calls another a function [say foo2()] which returns a clean html form in response to the call (when the shortcode is called).
SECTION 2: In this section I defined the function foo2(), within which the html form [login form] is defined and returned to foo1() [where it is displayed].
Then i created an action [ add_action('init', 'validate_login_user'); ] which will call the function validate_login_user() on initialization, inside this function i checked for isset(METHOD[username]) and isset(METHOD[password]) and then do respective logic.
Like this i created multiple [shortcodes] for each of the pages I wanted to create at the time of activation,
Then :
**step 1:** register_activation_hook(__FILE__,'activation_plugin');
**step 2:** activation_plugin(){
'390' => [ // '390' is page id
'post_title' => 'Page title say login',
'post_content' => "[user_login]",
'post_status' => 'publish',
'post_type' => 'page',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_category' => array(1)
],
'391' => [
'post_title' => 'page title 2',
'post_content' => "[short_code2]",
'post_status' => 'publish',
'post_type' => 'page',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_category' => array(1)
],
// like this add multiple shortcodes
}
// this foreach will create all the pages
foreach ($_ as $key => $value) {
$the_page_title = $value['post_title'];
$the_page_name = $value['post_title'];
// the menu entry...
delete_option($value['post_title']);
add_option($value['post_title'], $the_page_title, '', 'yes');
// the slug...
delete_option($value['post_title']);
add_option($value['post_title'], $the_page_name, '', 'yes');
// the id...
delete_option($key);
add_option($key, '0', '', 'yes');
$the_page = get_page_by_title( $the_page_title );
if ( ! $the_page ) {
$the_page_id = wp_insert_post( $value );
}
else {
$the_page_id = $the_page->ID;
$the_page->post_status = 'publish';
$the_page_id = wp_update_post( $the_page );
}
delete_option( $key );
add_option( $key, $the_page_id );
}
Create Page On Plugin activation with content
$page_content= 'Content of page';
$demo_page = array(
'comment_status' => 'closed',
'ping_status' => 'closed' ,
'post_author' => 1,
'post_content' => $page_content,
'post_date' => date('Y-m-d H:i:s'),
'post_name' => 'page_name',//display on address bar
'post_status' => 'publish' ,
'post_title' => 'Page Display Name',
'post_type' => 'page',
);
//insert page and save the id
$demo_page_value = wp_insert_post( $demo_page, false );
//save the id in the database
update_option( 'testpage', $$demo_page_value );
You Use and Guide and read this link How to Create plugin and pages https://codex.wordpress.org/Creating_Options_Pages

Categories