bnks.xyz

Menu

Tag: code

Bulk updating Cloudflare’s firewall rules

I’ve been using Cloudflare recently and overall it’s extremely straightforward, but there was one small fly in the ointment – the firewall settings.

Currently you can only enter single IPv4 addresses at once (ranges like x.x.x.x-x aren’t valid either). Luckily they have a very easy to use API so I put together a quick form that does a bulk update of your rules.

You can only do one “action” at once (whitelisting / blacklisting / removing), but if you need anything more complicated I’m surprised you’re here… The API is also smart enough that it doesn’t moan if you ask it to, e.g., whitelist an IP that is already whitelisted.

You can download it on GitHub, and as always pull requests are welcome.

RegEx to match / link Twitter usernames and hashtags

One of the things we do at Helpful Technology is provide digital skills training. As part of that we have a sandboxed Twitter style social network platform that allows users to get used to the medium and practice in a safe environment.

I’ve been re-building this system recently, and one feature we wanted to add was the automatic linking of usernames and hashtags. This is fairly simple to do with a bit of RegEx in PHP – and on WordPress you can just attach it to the filter called the_content to process all text before it is output and insert links as needed. The code could be put in a plugin, but here it’s written so you can just drop it in to your functions.php.

If you’re wondering about the ‘weird’ \p{X} statements – that’s using PHP’s unicode character properties to ensure all languages are matched.

Bash script to configure MySQL database and install WordPress

Updated 4/6/15 – now with added key/salt generation

When deploying a new WordPress site, there are two (potentially) needlessly time-consuming steps – logging in to MySQL to setup a new database and user, and then copying the WordPress files to the directory. The script below lets you do this in one step, pass in your database and basic WordPress settings, and it will configure MySQL and directly download WordPress to the server. A key benefit of this over a phpMyAdmin + FTP approach is that it will work over even ropey internet connections as the work is all done on the server itself.

A more advanced approach is to build a setup script based on WP-CLI, which allows the most customisation, but also needs more setup work and control over the server. If you always install the same plugins and themes – definitely look in to this.

How to use the script:

  1. SSH in to your server and navigate to the desired install directory (e.g. /var/www/domain/)
  2. Paste all of the following in to terminal and hit Return:
    curl -L -o 'wp.sh' https://gist.githubusercontent.com/emirpprime/37ef1f355ec5a7ecbb8f/raw/019acd595af695498acdf7b497e32ca0ebfb87f0/wp.sh && bash wp.sh
  3. Enter your details when prompted
  4. (Exit / close the SSH connection, your done here)
  5. Navigate to your new site in a web browser to complete the normal WordPress installation steps

Here’s the raw script so you can see what it’s doing – feel free to Fork it or suggest improvements:

Running the Gandi.net API on their PAAS

I recently setup a small server monitoring script using the APIs from Gandi and Monitor.us – both services I would recommend – to use as a module within Status Board.

There was one small quirk – I decided to run it from a Gandi PAAS instance and discovered that the PEAR install they provide doesn’t include two PEAR modules required to run their own API… Luckily it is easy to fix.

  1. Download HTTP_Request2 and Net_URL2 from PEAR and extract them locally.
  2. Navigate inside the extract folders and upload the HTTP and Net folders respectively to /lamp0/web/includes
  3. That’s it!

A strange oversight perhaps but at least Gandi provide the ready configured includes path/folder.

Resolving wpautop and Shortcode problems

Background

WordPress offers quite a friendly hybrid WYSIWYG / HTML editor that makes publishing easy even for those with no HTML experience. One of the ways it accomplishes this is through a function called wpautop. This runs on the_content as the page is drawn to insert <p> and <br/> tags as needed. By inserting them dynamically like this it prevents code creeping in to the editor and leading to potential confusion / corruption. However, it can sometimes lead to unexpected results – especially when combined with shortcodes.
Continue reading “Resolving wpautop and Shortcode problems”

Advanced CPT Filtering in WordPress

Prior to WordPress 3.1 you could only use one meta_key : meta_value : meta_compare in a query_posts. This meant that you could, for example, get the custom post type (cpt) product, filter it to get only those products that have a price >= 100. However you could not then orderby department ASC. Neither could you get the products with a price over 100 and colour green.
Now you can do advanced CPT filtering in WordPress since 3.1 with meta_query. There’s a nice intro post to this which I won’t repeat, but here is the code I used last week as an example:
Continue reading “Advanced CPT Filtering in WordPress”

Highlighting Custom Post Types in the menu

There seems to be a problem/oversight in WordPress at the moment (afaik).

You can generate Custom Post Types (CTP) easily enough, and have them visible on any page using templates/the loop, but when you click through to a single CTP WordPress assigns the current_page_parent to the default posts page. For many that will mean highlighting the News or Blog pages of their site.
After a lot of searching, and finding lots of people asking the same question but no solutions (except hacks), I found a reply by jonny_williams to a post about this on the wordpress.org/support forum. His solution at least runs from functions.php (and so before the content is displayed) rather than being accomplished through javascript like many others.

After a couple of modifications I settled on this:

function fix_portfolio_parent($menu){
	global $post;
	if ( 'projects' == get_post_type() )
	{
		$menu = str_replace( 'current_page_parent', '', $menu ); // remove all current_page_parent classes
		$menu = str_replace( 'menu-item-XXX', 'current_page_parent menu-item-XXX', $menu ); // add the current_page_parent class to the page you want
	}
	return $menu;
}
add_filter( 'nav_menu_css_class', 'fix_portfolio_parent', 0 );

Just replace the XXX with the number of the parent page you want to highlight, and 'projects' with the name of your CTP. As always, make sure you function name is unique and relevant. If you have multiple CTPs, either duplicate this for each, or extend the if (I haven’t looked at the performance differences of these options).

Conditionally loading plugin scripts in WordPress

I’m a big fan of WordPress – it’s both easy to use as an author and easy to develop for. There are also a huge amount of free (and paid) plugins to extend the core functionality.
Unfortunately there is a downside – most plugins do not conditionally load their files only on pages that call them. It is all to easy to end up with every page loading a string of Javascript and CSS files slowing the site to a crawl, when they are only actually required on specific pages.

I’m going to show 2 quick examples of how to use the functions.php file in your theme to only load the scripts for a plugin on a specific page using deregister and remove_filter, which are built into WordPress.
Continue reading “Conditionally loading plugin scripts in WordPress”

Tracking downloads in Google Analytics with jQuery

Google Analytics (and other similar services) aren’t 100% reliable, but they give a valuable insight into what the visitors to your site are up to. One thing they won’t do is track clicks on download links, unless you specify an onClick action for each link.

But here jQuery comes to the rescue – just a few lines of code will have all links to whatever filetypes you need tracked in Google Analytics as Events. Another common use of the same technique (with a few minor changes to the code) is to track clicks on outgoing links.
Continue reading “Tracking downloads in Google Analytics with jQuery”