I created a plugin with 'view page' that uses MYSQL + PHP. When I use wp_insert_post(), it turns everything to HTML
I created a custom plugin. The plugin has a view log page. Here is my viewpage_template.php. First part is a query. The second part is a PHP for loop to present the data in a table.
?php
// viewpage_template.php
global $wpdb;
// Get User ID
$journal_author = get_current_user_id();
// this adds the prefix which is set by the user upon instillation of wordpress
$table_name = $wpdb-prefix . sg_entries;
$prompt_table = $wpdb-prefix . sg_prompts;
// this will get the data from your table
$retrieve_data = $wpdb-get_results( SELECT * FROM $table_name WHERE journal_author = $journal_author );
$result = $wpdb-get_results(SELECT * FROM $prompt_table,ARRAY_A);
$prompt_text_db = $result
?
?php
// Simple table database
foreach ($retrieve_data as $retrieved_data){ ?
tr
td?php echo $retrieved_data-journal_date;?/td
td?php
$journal_day_of_year = $retrieved_data-journal_prompt_ID;
echo $prompt_text_db[$journal_day_of_year -1][journal_prompt_text];
?/td
td?php echo $retrieved_data-journal_content;?/td
tda href=#?php echo view; ?/a/td
tda href=#?php echo edit; ?/a/td
/tr
?php
}
?
In my main plugin, I created a function using wp_insert_post to create a page in WordPress that runs when someone installs my plugin. The page creates a static page of my view log page instead of a page that runs PHP code each time for a user. So, no matter if you're logged in or a different user, you see the contents of user id #1 (admin, me).
register_activation_hook( __FILE__, 'myplugin_activate2' );
function myplugin_activate2() {
//create a variable to specify the details of page
ob_start();
include('viewpage_template.php');
$journal_logs = ob_get_clean();
$post = array(
'post_content' = $journal_logs, //content of page
'post_title' ='logs-journal', //title of page
'post_status' = 'publish' , //status of page - publish or draft
'post_type' = 'page' // type of post
);
wp_insert_post( $post ); // creates page
}
I am wondering... 1) why does wp_insert_post retrieve my user id (admin id #1)? 2) Why is my view page a static page, instead of a dynamic-data page? I am guessing that wp_insert_post converts all my php from my template page to HTML? I want this page to run PHP code.
Topic wp-load.php mysql plugin-development plugins Wordpress
Category Web