WordPress is a popular blogging software, and it does some things well. The ability to download and install updates on its own is nifty. This is not 100% safe, but I use Drush for Drupal on occasion to perform the same task.
A recent project required integration between a WordPress blog and the custom site that I had to code. The same website had time-sensitive data that was to be published every night at midnight (outside of WordPress). An e-mail from the customer alerted me to the fact that this information was being published early.
Sure enough, a quick <?php print date('Y-m-d'); ?>
before the relevant code did return tomorrow’s date. Odd. After some digging, it was determined that this was not a configuration issue. The offending code came from the WordPress inclusion file:
define('WP_USE_THEMES', false);
require_once('./blog/wp-load.php');
wp-load.php
loads wp-config.php
, which in turn loads wp-settings.php
. This last file has this wonderful bit of magic:
if ( function_exists('date_default_timezone_set') )
date_default_timezone_set('UTC');
Basically, it does not matter what your WordPress settings say, this is going to change your timezone to UTC (as long as you use PHP 5.1 or newer). In my case, I simply added a line on each page that includes wp-load.php:
define('WP_USE_THEMES', false);rnrequire_once('./blog/wp-load.php');rndate_default_timezone_set('America/Phoenix');
Now that we have fixed a problem introduced by hacking WordPress, let’s do something fun:
<div id="block4">
<?php
query_posts('posts_per_page=1&post_status=publish&cat=1');
if (have_posts()) {
the_post();
?>
<h2>Today's BLOG: <a href="<?php the_permalink(); ?>"><? the_title(); ?></a></h2>
<?php
the_content();
print '<h2> </h2>';
$withcomments = 1;
comments_template();
}
?>
</div>
<script type="text/javascript">
// Iterate through Blog reply inputs...
var control = $('#block4 input:text');
for (var x = 0; x < control.length; x++) {
if (control[x].id == 'email') {
control[x].value = '<?php print htmlentities($fst->fetch_parent_email($_SESSION['id'])); ?>';
} else if (control[x].id == 'author') {
control[x].value = '<?php print htmlentities($_SESSION['first'].' '.substr($_SESSION['last'], 0, 1)); ?>';
}
control[x].type = 'hidden';
}
control.parents('form:first').append('<input type="hidden" name="redirect_to" value="<?php print $_SERVER['PHP_SELF']; ?>?task=blogcomment" />');
// Hide all of the labels for the above inputs
control = $('#block4 label');
control.css('display', 'none');
</script>
query_posts()
finds the blog post we are interested inhave_posts()
ensures that we have onethe_post()
prepares it for our usethe_permalink()
is a link to the blog post on the blogthe_title()
is the post titlethe_content()
– same ideacomments_template()
shows the comments (when$withcomments
is set) along with a comment entry form, if your WordPress settings allow it.
Now comes the dynamic part. Because this was on a page that users had already logged in to, I decided to fill in the user’s name and e-mail address for them so that users can recognize each others comments. The h2
tag was a CSS hack to insert a dividing line between post and comments.
For reasons that I won’t name here, there was another text input that was named email
so jQuery was used to find the correct HTML form items. #block4
is the containing div and input:text
is the text selector.
After it sets the text, it hides those controls. This leaves the labels visible. Thankfully, jQuery can handle this case as well, and easily. It only takes two lines (that could be merged) to hide all the labels in the #block4
div.
One more trick that is performed is to modify the comment form to redirect to the current page after a comment is posted. Surely you can think of a use for this. jQuery is very handy.
EDITED: Typo.