I am new to developing the wordpress plugin. I am developing a plugin and I have succeeded in creating a new page whenever the plugin is activated but I want to add a custom php template file to that page. Thats means on activation I want to create a custom page with a custom template. Is it possible to do?
To create a new page I have used this code but could not find anything on adding the template thing:
register_activation_hook(__FILE__,'my_plugin_install');
function my_plugin_install() {
global $wpdb;
$the_page_title = 'Custom Cart';
$the_page_name = 'cart';
delete_option("Custom Cart");
add_option("Custom Cart", $the_page_title, '', 'yes');
delete_option("cart");
add_option("cart", $the_page_name, '', 'yes');
delete_option("my_plugin_page_id");
add_option("my_plugin_page_id", '0', '', 'yes');
$the_page = get_page_by_title( $the_page_title );
if ( ! $the_page ) {
$_p = array();
$_p['post_title'] = $the_page_title;
$_p['post_content'] = "This text may be overridden by the plugin. You shouldn't edit it.";
$_p['post_status'] = 'publish';
$_p['post_type'] = 'page';
$_p['comment_status'] = 'closed';
$_p['ping_status'] = 'closed';
$_p['post_category'] = array(1);
$the_page_id = wp_insert_post( $_p );
}
else {
$the_page_id = $the_page->ID;
$the_page->post_status = 'publish';
$the_page_id = wp_update_post( $the_page );
}
delete_option( 'my_plugin_page_id' );
add_option( 'my_plugin_page_id', $the_page_id );
}
This is a simple way to create a page with attach template in WordPress. To create a new page I have used this code:
register_activation_hook(__FILE__,'my_plugin_install');
function my_plugin_install() {
$the_slug = 'cart';
$args = array(
'name' => $the_slug,
'post_type' => 'page',
'post_status' => 'publish',
'numberposts' => 1
);
$my_posts = get_posts($args);
if(empty($my_posts)){
$my_post = array(
'post_title' => 'Custom Cart',
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page',
'post_name' => 'cart'
);
// Insert the post into the database
$post_id=wp_insert_post( $my_post );
update_post_meta( $post_id, '_wp_page_template', 'page-templates/cart.php' ); // optional to add page template
}
}
Add custom page template: /theme/your-theme-name/page-templates/cart.php:
<?php
/*
* Template Name: Custom Cart Page
*/
get_header(); ?>
HTML code here ..
<?php get_footer();?>
Related
I have two custom post types with acf fields.
Custom Post_A, which has 2 fields - title / submitted
Custom Post_B, which has 2 fields - title / percent
Both Post_A and B have the same title (which is the logged in user's name) and they already exist.
When a field ‘submitted’ has the value ‘done’ in Post_A, I need to automatically update a ‘percent’ field with a value ‘50’ in Post_B.
I tried the following code but it doesn't update ‘50’ to Post_B.
Would you please correct my code?
$posts = get_posts(array(
'author' => get_current_user_id(),
'posts_per_page' => -1,
'post_type' => 'post_a',
'meta_key' => 'submitted',
'meta_value' => 'done'
));
$the_query = new WP_Query( $posts );
$the_count = count($the_query);
if($the_count>0) {
foreach ($the_query as $is_done){
$my_post = array();
$my_post['post_type'] = 'post_b';
$my_post['post_title'] = the_title();
// Update the post into the database
$field_key = "field_606cb546456343";
$value = "50";
update_field( $field_key, $value);
}
}
Thank you.
You can use save_post_{$post->post_type} action hook that will trigger on specific post type. check the below code.
function update_post_b( $post_id, $post, $update ){
$post_a_title = get_the_title( $post_id );
$posts = array(
'author' => get_current_user_id(),
'posts_per_page' => -1,
'post_type' => 'post_b'
);
$post_b = new WP_Query( $posts );
if( $post_b->have_posts() ){ while ( $post_b->have_posts() ) { $post_b->the_post();
if( $post_a_title == get_the_title() ){
update_post_meta( get_the_ID(), 'percent', 50 );
}
} }
}
add_action( 'save_post_post_a', 'update_post_b', 10, 3 );
USEFUL LINKS
save_post_{$post->post_type}
I have a problem in Wordpress. I want to make a hook on Contact Form 7, so that when the user clicks send, it first saves this information inside a custom post type.
Reading the documentation, I found this
// run the action
do_action( 'wpcf7_before_send_mail', $contact_form );
// define the wpcf7_before_send_mail callback
function action_wpcf7_before_send_mail( $contact_form ) {
//code
};
// add the action
add_action( 'wpcf7_before_send_mail', 'action_wpcf7_before_send_mail', 10, 1 );
But I do not know how to continue. Has anyone done this and can you help me please?
function action_wpcf7_before_send_mail( $contact_form ) {
$post_content = ''; // empty contebt
foreach ($_REQUEST as $key => $value) {
$post_content .= $key.': '.$value.'
'; //add each form field to content
}
$title = $_REQUEST['some field'].' '.$_REQUEST['some field2']; // generate dynamic title
$t = time();
$thash = md5($t);
$my_query = array(
'post_title' => wp_strip_all_tags( $title ),
'post_content' => $post_content,
'post_type' => 'your-post-type',
'post_name' => $thash,
'post_status' => 'publish',
'post_author' => 1
);
$data = wp_insert_post( $my_query );
return $contact_form;
};
// add the action
add_action( 'wpcf7_before_send_mail', 'action_wpcf7_before_send_mail', 10, 1 );
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' );
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