Posts Tagged ‘snippet’

16 June, 2014 — Development, Snippets, WordPress

Hide A WordPress Plugin From Plugin List

I was recently working on a client site, which required me to create a custom plugin for his website. I wanted to hide the plugin from the user, as he was a beginner and the plugin contained some sensitive functions to run his website.

In this snippet, I’ll show you how you can easily hide a WordPress plugin from the plugin list. The plugin will still work, but just won’t appear in the plugin list.

Just add the following snippet to your current theme’s functions.php file:

function hide_my_plugin() {
    global $wp_list_table;
    $hidearr = array('plugin-directory/plugin-file.php');
    $myplugins = $wp_list_table->items;
    foreach ($myplugins as $key => $val) {
        if (in_array($key,$hidearr)) {
add_action('pre_current_active_plugins', 'hide_my_plugin');

Replace plugin-directory/plugin-file.php in above code with your plugin’s directory and file name. You can find this info by clicking on edit plugin link from the plugin list.

What about Multisite?

If you wanna hide the plugin from your WordPress Multisite, then you above snippet will not remove the plugin from the Network admin list. Here’s a snippet which will work on the WordPress Multisite

function mu_hide_plugins_network( $plugins ) {
    // let's hide akismet
    if( in_array( 'akismet/akismet.php', array_keys( $plugins ) ) ) {
        unset( $plugins['akismet/akismet.php'] );
    return $plugins;
add_filter( 'all_plugins', 'mu_hide_plugins_network' );
9 January, 2014 — Development, Snippets, WordPress

Create a “Recent Posts” Module Outside of WordPress

Not all websites run on WordPress, but some websites might decide to use WordPress as their blogging choice. They decide they want to display the recent posts from the blog on the website. However the website isn’t in the WordPress core. So how do we display the recent posts on the website?

The Code

To be able to use WordPress outside of the WordPress core is simple. Just include the 

 file in your website:

// Include the wp-load from WordPress Install

// Get the last 10 posts
$recent_posts = wp_get_recent_posts(array(
    'numberposts' => 10

// Output the posts
echo '<ul>';
foreach($recent_posts as $post) {
    echo '<li><a href="', get_permalink($post['ID']), '">', $post['post_title'], '</a></li>';
echo '</ul>';

Once you have the 

 file included, the entire WordPress core is open to use!

5 November, 2013 — Development

Center a Block Element Without Knowing its Width

Centering an element may seem quite simple thinking that “text-align:center” will solve the problem. Or maybe set “width” and “margin: 0 auto”, but what happens when you need to center a block element with variable width?

W3C tells us that we can use “margin: 0 auto” to center tables so why shouldn’t we use display: table to center a fluid div:

#my-element {
  display: table;
  margin: 0 auto;

This can solve many problems and does a good job in some cases but you must be aware that “display: table” is not working when using our “beloved” Internet Explorer (IE5.5, IE 6, IE 7) so don’t count on this for main design elements (especially if this ruins your design).

I hope this was useful for you! If you have any comments or suggestions, do not hesitate to post them in the comments section.

Photo Credit: Zzpza

15 September, 2013 — Development, Snippets, WordPress

Display A Message On Older Posts

When blog posts get old, sometimes the information in them is not as accurate as they were at the time of writing. This can be misleading and can cause confusion to the reader.

This simple bit of code when either placed in your themes funcions.php file or a functionality plugin will display a message at the top of any posts that are older than 1 year. You can customise the message in the code below and add some styles to your style.css file. Once done upload your file WordPress will automatically take care of the rest.

// display a message on older posts
function tb_older_post_message () {
    $posted = get_the_time('U');
    $current = current_time('timestamp');
    //Convert difference in seconds to days
    $diffTime = ($current - $posted) / (60*60*24);
    if($diffTime > 365){
        echo '<div class=older-post-message>' . __('This post was written more than a year ago and <em>might</em> not be entirely accurate anymore.', 'wptips') . '</div><br />';
6 August, 2013 — Development, Snippets

Simple PHP Template Class

Update: Now available on GitHub here. Please commit improvements.

For a recent project of mine (newsletter system) I needed a simple but effective PHP class that would allow me to send a string of data and find every word with the curly bracket around it and replace it with a word or string.

For example in my newsletter system, I needed the email’s to be customized to the person it would be sending it to such as Dear Daniel… Dear James…. but during the loop to send the email I would need to get the script to switch {name} to be Daniel. To do this I found this amazing little PHP class that does just the job.


Click here to download Simple PHP Template Class.

The Usage

This class is very simple. You pass 3 variables into the function Replace.

  1. An array of keys and variables – the key will be what the script will look for in your template such as {name}. The value will be what {name} will be replaced with.
  2. The template string where the data will be replaced.
  3. If the template is case sensitive. Eg {Name} wouldn’t work in the below example where as {name} would.
// Array of variables to change, template string, case sensitive true/false
echo CurlyVariables::Replace(array('name' => $name, 'email' => $email), $string, true);

Any trouble please let me know in the comments below.

Photo by: Travis Isaacs

24 July, 2013 — Development, Snippets

Simple PHP MySQL Database Class

Update: Now available on GitHub here. Please commit improvements.

A while back now I decided to dive into OOP (Object-oriented programming) in PHP. Until then I had not really had any experience with OOP as functional PHP did the job just fine for me at the time. Now I have had some time getting to know OOP I realised why so many people now use it!

The first thing I wanted to learn was the basics and like with 90% of my projects this would involve a mySQL database connection and queries. That was where I began. Reading about the PDO class (a database package included in PHP) I quickly understood that using the class command “extends” I could write my own class to simplify my database interaction process.


Click here to download my mySQL database class. (Note: this class can be adjusted for other database types)

The Usage

Like with any class we need to initialize the class and assign it to a variable.

$database = new Database();

Then we can do any of the following…

Simple Insert Query
Simply insert records to a table.

// Simple Insert Query
$database->query('INSERT INTO mytable (FName, LName, Age, Gender) VALUES (:fname, :lname, :age, :gender)');

// Bind the data
$database->bind(':fname', 'John');
$database->bind(':lname', 'Smith');
$database->bind(':age', '24');
$database->bind(':gender', 'male');


Insert Multiple Records using a Transaction
You can insert multiple records using the transaction function. This allows you to run 1 query and multiple binds to insert a lot of rows at once.

// Begin the Transaction.

// Query
$database->query('INSERT INTO mytable (FName, LName, Age, Gender) VALUES (:fname, :lname, :age, :gender)');

// Bind the Data
$database->bind(':fname', 'Jenny');
$database->bind(':lname', 'Smith');
$database->bind(':age', '23');
$database->bind(':gender', 'female');

// Execute that data

// Bind the next set of data
$database->bind(':fname', 'Jilly');
$database->bind(':lname', 'Smith');
$database->bind(':age', '25');
$database->bind(':gender', 'female');

// Execute that data

// End the transaction

Get Last Insert ID
Simple – Get the ID of the last record inserted.

echo $database->lastInsertId();

Select a Single Row
Select just a single row of data from a table.

// Query
$database->query('SELECT FName, LName, Age, Gender FROM mytable WHERE FName = :fname');

// Bind data
$database->bind(':fname', 'Jenny');

// Next we run the single method and save it into the variable $row.
$row = $database->single();

echo "
echo "

Select Multiple Rows
Get a selection of rows from a table and add them to an array ready to be processed using foreach().

// Query
$database->query('SELECT FName, LName, Age, Gender FROM mytable WHERE LName = :lname');

// Bind data
$database->bind(':lname', 'Smith');

// Run the resultSet method and save it into the $rows variable.
$rows = $database->resultset();

echo "
echo "

Count Rows

// Display the number of records returned
echo $database->rowCount();

I hope you find this script useful. Please comment if you have any questions or found this useful.

Photo by: Chris Hsia