I'm creating an APP with a Wordpress child theme. It has a start page created with wp_insert_post when activating the theme. How do I set this page as startpage with PHP in functions.php?
// Install theme
if ( is_admin() && isset($_GET['activated'] ) && $pagenow == 'themes.php' ) {
$last_id = wp_insert_post(array(
'post_type' => 'page',
'post_title' => 'Welcome to this wonderful page!',
'post_content' => 'Holy smoke',
'post_name' => 'startpage',
'post_status' => 'publish',
'comment_status' => 'closed'
));
update_post_meta($last_id, "_wp_page_template", "page.php");
// Set this page as startpage... but how?
} // Install theme
If understood correctly, you are searching for the option page_on_front:
Just use:
// Set "static page" as the option
update_option( 'show_on_front', 'page' );
// Set the front page ID
update_option( 'page_on_front', $last_id );
Related
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.
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.
I am currently making a Wordpress plugin, but I couldn't find an answer to this.
How do you add a page upon activation of the plugin?
I have added posts upon activation earlier with the wp_insert_post function, but I can't find a way to insert a page.
You should use the activation hook for plugins in order to make any actions upon the plugin activation.
register_activation_hook( __FILE__, 'activation_hook_callback');
function activation_hook_callback()
{
//add the post type and other options in the array for the query
$page = array(
'post_status' => 'publish' ,
'post_title' => 'Page name',
'post_type' => 'page',
);
//add the page and ID will be saved.
$the_page_itself = wp_insert_post( $page );
}
This should work.
I solved it. Wordpress has several post-types:
Post (Post Type: 'post')
Page (Post Type: 'page')
Attachment (Post Type: 'attachment')
Revision (Post Type: 'revision')
Navigation menu (Post Type: 'nav_menu_item')
To add a post upon activation of my plugin:
function add_page_upon_activation() {
$arr = array(
'post_title' => 'title',
'post_name' => 'slug',
'post_status' => 'publish',
'post_type' => 'page',
'post_content' => 'yes, a nice page',
);
wp_insert_post($arr);
}
add_action( 'activated_plugin', 'add_page_upon_activation' );
How to create custom wordpress page after install my custom integration wordpress plugin.
Ex : (If we have install Woocommerce it's automatically create the cart & checkout & myaccount page). I need like some format.
You can create new page on plugin activation hook in your plugin
function add_my_custom_page() {
// Create post object
$my_post = array(
'post_title' => wp_strip_all_tags( 'My Custom Page' ),
'post_content' => 'My custom page content',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page',
);
// Insert the post into the database
wp_insert_post( $my_post );
}
register_activation_hook(__FILE__, 'add_my_custom_page');
try this to your plugin
It will check that a page with the same name does not exist.
register_activation_hook( __FILE__, 'custom_plugin_activation' );
function custom_plugin_activation() {
if ( ! current_user_can( 'activate_plugins' ) ) return;
global $wpdb;
if ( null === $wpdb->get_row( "SELECT post_name FROM {$wpdb->prefix}posts WHERE post_name = 'new-page-slug'", 'ARRAY_A' ) ) {
$current_user = wp_get_current_user();
// create post object
$page = array(
'post_title' => __( 'New Page' ),
'post_status' => 'publish',
'post_author' => $current_user->ID,
'post_type' => 'page',
);
// insert the post into the database
wp_insert_post( $page );
}
}
Here is a full list of parameters accepted by the wp_insert_post function.
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' );