I think, therefore I am. I am, therefore I sail

Category: PHP Page 3 of 11

WordPress: How to easily remove the ‘App Shop’ link in The Events Calendar plugin

The Events Calendar by Modern Tribe is a great way to create and manage events. Modern Tribe has several sweet add-ons that help with ticket sales, conference management, etc. To make it easier for users of their plugin to get add-ons a convenient ‘App Shop’ link is provided in the WordPress admin, under the Events menu. There may be instances when you you do not want to show this link, however. The following code snippet can be dropped into a plugin or functions.php file to safely remove the link. It even ensures future compatibility by tapping into The Events Calendar libraries to ascertain the correct menu item to remove, instead of having it statically set.

/**
 * Removes the App Shop submenu item under the Events submenu.
 * NOTE: This only works with The Events Calendar plugin!
 * 
 * Need to pull in some of the tribe objects to get at the menu slug. It is
 * important to do it this way so that if the slugs are updated in the future
 * this function will still work
 */
function remove_tribe_app_menu_link() {
    if ( !class_exists( 'TribeEvents' ) ) 
                require_once( WP_PLUGIN_DIR . "/the-events-calendar/lib/the-events-calendar.class.php" );
    if ( !class_exists( 'TribeAppShop' ) ) 
                require_once( WP_PLUGIN_DIR . "/the-events-calendar/lib/tribe-app-shop.class.php" );

    $where = 'edit.php?post_type=' . TribeEvents::POSTTYPE;
    remove_submenu_page( $where, TribeAppShop::MENU_SLUG);
}
add_action( 'admin_menu', 'remove_tribe_app_menu_link', 100);

PHP: Strip number from string

I recently ran across a situation where a string being used as a key contained numeric characters. The requirements specified stated that the new data structure keys do not contain numbers. This is a very simple problem to solve and can be done with a simple PHP str_replace. The following function is what I came up with.

function trim_numbers($str) {
    $n = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
    return str_replace($n, '', $str);
}

Introducing the Meetup API client for PHP

I am working on a pretty major project right now and one of the requirements is the ability to interact with the Meetup API. I looked around and found one existing Meetup client in PHP, however it is horribly architected and as I started using it thinking I could improve it I decided it would be too much work and scrapped it to build my own. This new client is much more compact and efficient. The excerpt from the Meetup API client for PHP website says:

Meetup (http://meetup.com) is a social networking site based around community groups. Meetup provides an API to access their platform services from remote applications to manage authentication, events, rsvps and more.

Until now a good PHP client for the Meetup API has not existed. This project aims to bridge that gap by providing a high quality stand alone Meetup API PHP client. This client is simple to use through provided classes and also allows powerful advanced usage through direct queries to the API

The client is quite simple to setup and get running in your application quickly. Most of the GET endpoints are currently supported with plans on implmenting the rest and support for POST on the way. Check the following resources to get up and running.

You can get the client from the Meetup API client for PHP Github project page

Please try it out and leave feedback on issues and desired additions on the project issues page

P.S. oAuth support is currently in the works!

WordPress: Retrieve posts for last N minutes or timespan

I am working on a project that requires me to pull the last 15 minutes worth of posts from a custom post type out of the wp_posts table for analysis. I thought this would be easy and quick, however I spend around 20 minutes digging until I finally found a nugget way at the bottom of the WP_Query codex page. Web searches and looking through the post functions unfortunately got me nowhere. I hope that if you are looking to do the same thing this post will be a helpful time saver to you.

The following will return posts just from the last 15 minutes

// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
	// posts in the last 15 minutes
	$where .= " AND post_date > '" . date('Y-m-d', strtotime('-15 minutes')) . "'";
	return $where;
}

add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );

The following will return posts from a timespan of 30 to 60 days old

// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
	// posts  30 to 60 days old
	$where .= " AND post_date >= '" . date('Y-m-d', strtotime('-60 days')) . "'" . " AND post_date <= '" . date('Y-m-d', strtotime('-30 days')) . "'";
	return $where;
}

add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );

Remember, it is good etiquette to prefix the name of your functions with the name of your theme or plugin. So if your plugin is named "Cat Namez" instead of using the filter_where function name you could use catNamez_filter_where. It is a bit longer, however it prevents namespace collisions with other plugins and possible fatal errors or stack overflows.

Be sure to read the WP_Query page on the WordPress Codex too! It is where I stole this code from 🙂

Introducing paz: a simple packaging and deployment tool for PHP projects on Windows Azure

Update (04-30-2012): paz has been combined with other tools into the WAZ-PHP-Utils project on Github. Please see there for further information.

These days PHP works well on Windows Azure, and there are some great tools available in the Windows Azure SDK for PHP that will help you build, package and deploy your applications. Even though there are some excellent tools available in the PHP SDK they still require a good amount of effort and complexity to properly get from your PHP application running locally in Windows Azure. With that in mind I set out to create a tool that takes the headache out of getting your existing PHP application off your hard drive and into Windows Azure with paz. paz is a simply packaging and deployment tool for PHP projects on Windows Azure. Simply give it the path to your project and let the magic happen. In a few short seconds you will have either a project running in a local Windows Azure development environment or a package ready to be  uploaded to Windows Azure itself. The following is an example of a command that will create a deployment package:

paz -in path/to/sources

After the command runs you will be able to find the package in path/to/sources/build.

paz is still in the infant stages but if you are doing work around Windows Azure with PHP I highly encourage you to check out this simple and easy to use tool. Send me some feedback on Github with bugs, likes, dislikes and feature requests. I also highly encourage you to read the tutorials put out by the Microsoft Interoperability Team about PHP on Windows Azure on the AzurePHP website

You can find documentation and sorce code for paz on the Github project page at http://github.com/blobaugh/paz

In this release paz does not provide the functionality to deploy that package it creates but there is hard work being done to implement it!

Page 3 of 11

Powered by WordPress & Beards