Authorising WordPress Users From Another Script

Sometimes you might want to add additional pages or some extra functionality outside of WordPress. I needed to do this and only allow singed in users to access the page.

There’s a pretty straight forward way to check visitors to the page simply by including the WordPress header file and then checking for an appropriate permission.

Read on for the script. Continue reading Authorising WordPress Users From Another Script

XMLDom::Save() Permission Problem

I had been hitting my head against the wall when I couldn’t get an edited XML file to save due to file permissions, even though I know that it was OK.

For some odd reason, when I went to save the XML file, it would try to write the file to the root directory, instead of the current working directory (where the file is read from).

As such, using realpath() to keep the complete system path to the XML file when you load it will make sure that DOM isn’t trying to save to the incorrect directory:

$myfile = 'myxml.xml';
$myfile = realpath($myfile);
$doc = new DOMDocument('1.0');
$doc->load($myfile);

// Let's just add a couple of elements for good measure
$root = $doc->documentElement;

$title = $doc->createElement('title');
$title = $root->appendChild($title);

$text = $doc->createTextNode('This is a title');
$text = $title->appendChild($text);

$doc->save($myfile);

A quick way to handle both strings and arrays in PHP

Something that happens occasionally is that I need to create a function that will specifically output chunks of HTML.

A good method is to allow for functions to handle both single strings and arrays. The problem? I don’t like duplicating code. The solution? A quick convention to an array!

function arrayish($a)
{
  $a = (array) $a;

  foreach ($a as $b) {
    echo "<p>$b</p>";
  }
}

That’s it. Nice and simple!

If someone knows a more efficient way to do this, I’d love to know.

Taking care of HTML comments in PHP

A little problem came up with some user submitted content on a platform I’m working with.

A form allows users to submit content with tinyMCE. If the content is pasted from MS Word, the source is then littered with HTML conditional comments that can have a detrimental effect on the the page that returns it.

After discovering what was going on, I thought that the best time to capture the offending content is when the text is submitted. Using a regular expression, I can capture the HTML conditionals as well as remove any unnecessary comments:

<code>
function clear_html_comments($html)
{
    return preg_replace('/&lt;!--(.|\s)*?--&gt;/', '', $html);
}
</code>

That’s it. Just pass in the content from tinyMCE and it should prevent any content being returned that is in HTML comments.

Life and Blogging is Hard

Plus.net have moved to the new CGI platform after some sort of disaster. As this was coming for almost a year, I’m shocked that the platform still seems in such a shoddy state.

This has meant that the site has been misbehaving for a few weeks. Most notably causing random Apache errors.

I think that it’s a wake up call to me that I need to get this blog living somewhere else. Letting it expand and breathe as it needs to.

Some of the way to that is getting a new hosting plan sorted out. I wanted to do this in an el-cheapo style by using SQLite instead of mySQL to power WordPress, but it seems like the database abstraction that it uses might as well be non-existant. mySQL or bust I’m afraid.

In a time when PHP provides a unified access library, and many frameworks have a respectable set of DB abstraction libraries – it’s frustrating to see the WordPress team get suckered into making new features the top of the development priority list, while failing to solidify or revisit programming fundamentals.

And what’s the point of having plugins at all if they don’t get approved in any way? After my trial with SQLite, I’m genuinely worried about any WP plugins now.

Moving soon folks!