i have created a plugin that creates a page on activation.
now i cant find a way to update the content of the page programitically
i have been searching the internet for a while could not find an answer
here is how i create a new page using my plugin
function some_function()
{
$post_details = array(
'post_title' => 'Page title',
'post_content' => 'Content of your page',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page'
);
wp_insert_post( $post_details );
}
register_activation_hook(__FILE__, 'some_function');
Related
I have made an action that allows a page to be made if the switch in the setting is checked. After it is confirmed it's checked it does create the page, but without the template added. How do I get my template to work?
add_action( 'admin_init', 'cart_page' );
function cart_page() {
//Add cart page to website
if ( get_option( 'cart' ) === "checked" AND get_option('cart_exist') === false) {
// IF CART HAS BEEN CHECKED
$new_page_id = wp_insert_post( array(
'post_title' => 'Cart',
'post_type' => 'page',
'post_name' => 'Cart',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_content' => '',
'post_status' => 'publish',
'post_author' => get_user_by( 'id', 1 )->user_id,
'menu_order' => 0,
// Assign page template
'page_template' => plugins_url('page_templates/cart_template.php', __FILE__ )
) );
wp_insert_post($new_page_id);
update_option( 'cart_exist', true );
}
else if (get_option('cart') === "" AND get_option('cart_exist') === true) {
// IF CUSTOMER DOES NOT WANT CART
update_option( 'cart_exist', false );
}
}
This is what my template page looks like in the plugin.
get_header();
echo do_shortcode( '[cart_portal]' );
get_footer();
?>
In my case, I have figured out a solution for the simple problem. If I just need to add a shortcode I can add it to 'post_content'. I would still like to know how to add template. But you can use a file to import the entire layout if you'd like still. But it cannot be used with visual composers.
See example below...
$new_page_id = array(
'post_title' => 'Cart',
'post_type' => 'page',
'post_name' => 'Cart',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_content' => '[cart_portal]',
'post_status' => 'publish',
'post_author' => get_user_by( 'id', 1 )->user_id,
'menu_order' => 0,
);
wp_insert_post($new_page_id);
so i want to add post programatically with my plugin, i write this code:
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'draft',
'post_author' => get_current_user_id(),
'post_category' => array(1)
);
// Insert the post into the database
$post_id = wp_insert_post( $my_post );
and this is return post ID to variable but post doesnt show in wp admin posts table. WordPress is counting this like new post but doesnt show. Why?
I'm learning to developing plug-ins for Wordpress. After activating my plug-in it creates a new page with the following code:
function add_page(){
global $wpdb;
$page = array(
'post_title' => 'test',
'post_content' => 'HERE I WANT PHP CODE',
'post_status' => 'publish',
'post_author' => get_current_user_id(),
'post_type' => 'page',
);
// insert post into the database
wp_insert_post( $page );
}
What is the cleanest and proper way of adding php to 'post_content'?
I want to make a user-overview page by querying the database.
I am not very familiar with WordPress development, basically i have a separate PHP script that i wan't to be able to pass form data from it to a WP Plugin and the plugin will get that data via $_GET or so and insert it to WP as a new post.
I have tried the below code within the plugin which works, however causes errors regarding "called to undefined function is_user_loggedin".
$post = array(
'post_title' => "test title",
'post_content' => "test content",
'post_date' => 0,
'post_date_gmt' => 0,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(1)
);
wp_insert_post($post);
I then found a solution to this, wrap the code within a function and execute it with the "init" hook.
function insert_post(){
$post = array(
'post_title' => "test title",
'post_content' => "test content",
'post_date' => 0,
'post_date_gmt' => 0,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(1)
);
wp_insert_post($post);
}
add_action('init', 'insert_post');
Again, this inserts the post but it constantly keeps inserting the same post over and over rather than only inserting it once when my plugin is called.
Any help or solution to this would be appreciated, thank you.
I dont use Wordpress. but When i look at the
Wordpress WP Insert Post Guidance
You should not wrap the Code in Function.
Kindly re check Whether you have included all the required includes/required files to call the wp_insert
EX:
// Gather post data.
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 )
);
// Insert the post into the database.
wp_insert_post( $my_post );
that
wp_insert_post() passes data through sanitize_post(), which itself handles all necessary sanitization and validation (kses, etc.).
according to the wordpress Guidance
I am developing a Wordpress plugin. I have some links for showing product Brands name and Product categories listing, in the url: http://testsite.com/product-categories and http://testsite.com/brands like that. So I want this link will be auto generated when plugin is activated and place in the default menu in Wordpress. How can I do that. I tried this code but its not working.
register_activation_hook( __FILE__, array( $this, 'myplugin_activate' ) );
public function myplugin_activate() {
$product_categories = array(
'post_content' => '',
'post_name' => 'product-categories',
'post_title' => 'Product Categories',
'post_status' => 'publish',
'post_type' => 'nav_menu_item',
);
$brands = array(
'post_content' => '',
'post_name' => 'brands',
'post_title' => 'Brands',
'post_status' => 'publish',
'post_type' => 'nav_menu_item',
);
wp_insert_post( $product_categories );
wp_insert_post( $brands );
}
Can anyone tell me how to do that? One thing I can not do that as page post type.
Searching for wp_insert_post and nav_menu_item, I found Programmatically add a Navigation menu and menu items where wp_create_nav_menu is suggested.
Should be a matter of using the functions wp_get_nav_menu_object, wp_create_nav_menu, wp_update_nav_menu_item and get_nav_menu_locations.