create dynamic page on plug-in activation - php

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.

Related

wp_insert_post doesnt show in WP Admin panel

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?

wp_insert_post running multiple times

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

Having more than one multiple field to post into "post_content"

I'm trying to make a form application with multiple fields to write info in.
Heres the code im working on right now.
$type = trim($_POST['Type']);
$new_post = array(
'post_title' => $title,
'post_content' => $_POST['description'],
'post_parent' => 65,
'post_status' => 'publish',
'post_type' => 'topic'
);
$topic_meta = array(
'forum_id' => $new_post['post_parent']
);
$topic_id = bbp_insert_topic($new_post, $topic_meta);
wp_redirect(get_permalink($topic_id));
exit;
I have more fields which are documented by the name customfieldone and customfieldtwo which I would like to add to the topic it creates from the form.
With that code above it only posts the text which is in description into the topic.

how to edit wordpress page from backend

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');

How to add menu link when activate the wordpress plugin?

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.

Categories