0%
Loading ...

Tutorial: Develop Plug-in that displays list of Posts under Admin-Settings in 8 Steps

Images (5)

General: Here a plugin that will display a list of posts will be demonstrated.

Read more about developer resources here: https://developer.wordpress.org/

Top-10-WordPress-Plugins-That-You-Need-To-Be-Using-In-2014

 

Step 01: Plugin name will be DisplayListOfPosts

Step 02: Create a directory under wordpress-root\wp-content\plugins\DisplayListOfPosts

Step 03: Create a file under that directory called: DisplayListOfPosts.php

Step 04: The the following content to the plugin file:

<?php

/*
Plugin Name: Display List of Posts
Plugin URI: http://www.flash-jet.com
Description: A plugin demo that will display list of Posts
Version: 1.0
Author: Gabriel Magen
Author URI: http://www.flash-jet.com
*/

?>

Step 05: From wordpress admin panel, activate the new plugin.

Step 06: Here an action of adding option to admin menu will be demonstrated. Add the following code to file:

//adding action to admin menu and bind it to function name

add_action(‘admin_menu’,’DisplayListOfPosts_admin_actions’);

function DisplayListOfPosts_admin_actions() {

add_options_page(‘Display List Of Posts’,’Display List Of Posts’,’manage_options’,__FILE__,’DisplayListOfPosts_admin’);

//add_options_page(HTML Page title,Settings Submenu,hook only admin can see,php constant,function that will display the posts list);

}

function DisplayListOfPosts_admin() {

}

Step 07: Replace function DisplayListOfPosts_admin()  with the following::

function DisplayListOfPosts_admin() {

global $wpdb; //wpdb wordpress database class
$mytestdata=$wpdb->get_results(“select ID,post_title from $wpdb->posts”); //get a query into wpdb class
echo “<br>Display list of posts:<br>”;

echo “<table border=1>”;//display the results as a table

foreach ($mytestdata as $singleraw) //loop inside the query
{
echo “<tr>”;
echo “<td>”.$singleraw->post_title.”</td>”;
echo “<td>”.$singleraw->ID.”</td>”;
echo “</tr>”;
}

echo “</table>”;

}

images (6)

 

Step 08: Final code:

<?php

/*
Plugin Name: Display List of Posts
Plugin URI: http://www.flash-jet.com
Description: A plugin demo that will display list of Posts
Version: 1.0
Author: Gabriel Magen
Author URI: http://www.flash-jet.com
*/

add_action(‘admin_menu’,’DisplayListOfPosts_admin_actions’);

function DisplayListOfPosts_admin_actions() {

add_options_page(‘Display List of Posts’,’Display List of Posts’,’manage_options’,__FILE__,’DisplayListOfPosts_admin’);
}

function DisplayListOfPosts_admin() {

global $wpdb;
$mytestdata=$wpdb->get_results(“select ID,post_title from $wpdb->posts”);
echo “<br>Display list of posts:<br>”;

echo “<table border=1>”;
foreach ($mytestdata as $singleraw)
{
echo “<tr>”;
echo “<td>”.$singleraw->post_title.”</td>”;
echo “<td>”.$singleraw->ID.”</td>”;
echo “</tr>”;
}
echo “</table>”;

}
?>

Based on: