:::: MENU ::::
Posts tagged with: wordpress

Adding Video Support to WordPress Media Gallery

WordPress Galleries

WordPress comes out-of-the-box with hundreds of really well implemented features. It’s one of the reasons it is the leading blogging software used today. One of the features we will focus on is the built in WordPress Gallery.

To add a gallery to a post, you can follow the below steps:

Step 1: Click Add Media

Click Add Media

Step 2: Click Create Gallery and select your photos

Create Gallery

Step 3: Edit and Insert

Insert Into Post

If you have noticed though, you can only add Images. If you have uploaded videos to your Media, you cannot add them to a gallery.

The Fix:

The change we need to make is located within the function wp_ajax_query_attachments(). This function is located within the file, wp-admin/includes/ajax-actions.php. You can do a search for this function, or look at or around line 1835.

Note: The only way to easily support videos within a gallery through WordPress’s default media gallery is to edit a core file. That means that after an upgrade, you may lose this change.

Specifically, change line 1841:

's', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type',

To:

's', 'order', 'orderby', 'posts_per_page', 'paged',

Removing the 'post_mime_type' array index from this query will not add a constraint your media library to image/% mime types.

Summary

If you go back to a post, you can add or edit a gallery and now videos will display. By default, thumbnails are not created for your videos which you upload. I would suggest the Video Embed and Thumbnail Generator plugin to handle this.


Infinite Redirect Loop during WordPress Login

The Other Loop

WordPress has a very intuitive and straight forward admin interface. This is one of the reasons I choose to deploy client projects on WordPress. It’s abilities have made it one of the leading software packages for content aggregation. Although it’s almost perfect, there are still some things that users and administrators may over look which could lead you to experience a bug. A infinite redirect loop when logging in could be one of them. Luckily for them, I get to work out all the bugs before the keys are handed over.

The Fix

This fix is pretty easy. What happens depending on your configured settings and what URL (including subdomain) you access the login page from, you confuse WordPress. WordPress knows it needs to authenticate the user, but if this is happening after the login form already posted, you get stuck in an infinit loop.

Add the following just below the RewriteBase directive in your sites .htaccess:

RewriteRule ^wp-admin - [L]

PHP FastCGI POST Requests Failing

The FastCGI Issue

I was working with a site that had to be migrated from a Virtual Dedicated Server to a GoDaddy 4GH plan and ran into an issue where POST forms passed through mod_rewrite were not being populated to $_POST.

I realized that in a FastCGI environment the requests will be redirected as a GET string, meaning that your POST data will be lost. It was a 1-liner in the ‘.htaccess’ file.

The Fix

If you have a R=301 in the brackets [], remove it so it looks like below:

# On FastCGI replace the last line above with this:
RewriteRule ^(.*)$ index.php?/$1 [L]

Enjoy.


Gotcha: WordPress Option Form Not Posting

The Issue – Form Not Posting

I was working on creating a theme with an Admin Options page for WordPress but ran into an issue from the article I was following where the form wouldn’t post. Normally, this is because there is either no action, or no interpreter for the _GET or _POST. Knowing this, I noticed there was no form action. Easy fix.

With WordPress, your actions are managed by your URI, example:

function.php?page=called.php

When you bring that to different environments, not all servers will handle the request within the same $_SERVER variable. S

o what’s the best way to POST your form the page serving you?

The Fix

You can fix it by using a combination of substr and strrpos.

substr, or sub string, will allow you to return only the characters within the specified offsets. strrpos, or string reverse position, will return the first integer position of the character you specified, starting from the end of the string.

Understanding your URI, ex: http://www.highonphp.com/wordpress/wp-admin/admin.php?page=functions.php, you know that the page you need to post your form to is admin.php?page=functions.php.

So, by using the following function, we will get the position of the / after wp-admin/ and use that as the starting point for substr.

echo substr($_SERVER['REQUEST_URI'], strrpos($_SERVER['REQUEST_URI'], '/')+1);

When you put it all together the FORM tag looks like this:

<form method="post" action="<?=substr($_SERVER['REQUEST_URI'], strrpos($_SERVER['REQUEST_URI'], '/')+1);?>">

Enjoy.


Create WordPress/CI Like Slug

Creating a Slug

I was working on a project that used a router to handle the URI requests. It was similar to Code Igniter and WordPress where I had a need for a slug. The following worked perfectly!

The Code

// $slug = Your Subject To Sluggify
// $hyphenate = Set true if you wish to NOT turn whitespace into hyphens
function create_slug($slug, $hyphenate = true){
     $slug = strtolower($slug);

     if($hyphenate){
         $slug = preg_replace("/[-sW]/","-",$slug);
     }

     return preg_replace("/[^a-z0-9-]/", "",strtolower($slug));
}

Note:Function updated on 2-Feb 2012 to enable space-hyphen auto replace by default

Examples

echo create_slug('Here Is An Example);
// returns: here-is-an-example

echo create_slug('ThisIs (Character) !intensive!!', false);
// returns: this-ischaracterintensive

Enjoy!