Search:

get the top parent category in WordPress

Here’s a cool little scrap of code to get the name of the top parent category in WordPress. This is another one of those things i spent a good deal of time trying to find through a lot of web searching. I finally came across a post somewhere (can’t remember where anymore) which had the solution I was looking for. I’ve just changed some of the wording (for my own benefit) and have posted it here. This will get the top parent category name, no matter how many levels deep in the category heirarchy you might be. To get this up and running, just add the code below to your functions.php file…

<?php
function get_parent_category_name($cat) {
    $parentCategoryList = get_category_parents($cat, false, ',');
    $parentCategoryListArray = split(',', $parentCatList);
    $parentName = $parentCategoryListArray[0];
    $stuffToReplace = array(' ' => '-', '(' => '', ')' => '');
    $parent = strtolower(strtr($parentName,$stuffToReplace));
 
    return $parent;
}
?>

Then, put this bit in your template where you want to place the top parent category name…

<?php get_parent_category_name($cat); ?>

This function returns the category “slug”.
To return the actual category name, replace the last line in the block of code above with this…

    return $parentName;

Have fun…

 

WordPress & bbPress integration login trouble?

I was just recently working on a project where i was building a new WordPress site and was adding a “forums” section powered by bbPress. I followed all of the instructions provided on the bbPress site – here.

I installed the “bbPress Integration” plugin and filled in all of the proper fields in the “bbPress Integration” screen under “Settings” in the WordPress admin. Likewise, I completed all of the necessary fields on the “WordPress Integration” screen under “Settings” in the bbPress admin. I then made sure to open up both of my config files (config.php for WordPress and bb-config.php for bbPress) and ensured that they had all of the proper matching keys/strings filled in for all of the “SALT” and “AUTH” values, etc., etc. – but no matter what i tried, I couldn’t get logins across both areas of the site to work. The cookies were not being used/written/shared across both platforms like they are supposed to if you do everything correctly.

When I logged into WordPress and then switched over to the bbPress-powered forums I discoverd that I was now logged out. Then I logged in to the bbPress-powered forums and switched over to the WordPress site and I was logged out again. I couldn’t get it remember/use/recognize the same cookies across both applications and it was driving me crazy.

What made matters worse is that this all worked perfectly on my local dev environment – running a MAMP server on my Mac.

Then, FINALLY, I stumbled across a few snippets here and there after doing A LOT of googling that mentioned an issue where using 64-character keys seemed to be the problem. Several users mentioned that switching the keys (SALT, AUTH values, etc.) to 32-character values seemed to make things work properly. So, i tried it and all of a sudden, everything now worked!

Whenever I come across a strange problem (like this one) that takes me hours to figure out, I try to make the effort to then share the solution here, so that others don’t have to go through the same hell as me.

So, to give you an example – take the code that contains your keys in your WordPress config.php file…


define('AUTH_KEY', '/xhuN*9-eSK//l}W-a=21z*+i6iz@N-6kWlTah[{gh}7{`%T~&B-HrYr5pgP+OxN');
define('SECURE_AUTH_KEY', 'l9jwMX2U*+:sSzQo~8OOt}!54*1KV@:}-*');
define('LOGGED_IN_KEY', '!]-^))}7PB_IZ+kzZV?H@sXvRC[K?:![lsd<6=4;UMKL_%Il%-axHv4*[g20m*g^');
define('NONCE_KEY', '!.OW{67^@=BM-THdr`2i<2qH,t_|/7$|-nUw4+os8n@*[ZF/nnHc`c%vsI~hM?2#');
define('AUTH_SALT', 'Jiz70.vxul~rpgfA#ll?:t24rF define('SECURE_AUTH_SALT', 'Jf7,nC+C,*&!cX^~T#&Y6L,)1`%6y%+++4x}HLFX6M^*GYrDJ4ih?x-vW#NnJ?$T');
define('LOGGED_IN_SALT', '4+NR?-=*ykRsY~:mqj`5#9!=hRfSi<#&C%nLS&u^p/JuPar)#Uj(j91JG|ThKjfY');
define('NONCE_SALT', 'sF=0{^1Y&WxIyd1Zc{*7&V~v+y6j0V0A}#R==J,-Ql8t[$+ :jV-t5PR~xA+[M%U');

and shorthen the 64-character strings down to 32-character strings, like so...


define('AUTH_KEY', '/xhuN*9-eSK//l}W-a=21z*+i6iz@N-6');
define('SECURE_AUTH_KEY', 'l9jwMX2U*+:sSzQo~8OOt}!54*1KV@:<');
define('LOGGED_IN_KEY', '!]-^))}7PB_IZ+kzZV?H@sXvRC[K?:![');
define('NONCE_KEY', '!.OW{67^@=BM-THdr`2i<2qH,t_|/7$|');
define('AUTH_SALT', 'Jiz70.vxul~rpgfA#ll?:t24rF define('SECURE_AUTH_SALT', 'Jf7,nC+C,*&!cX^~T#&Y6L,)1`%6y%++');
define('LOGGED_IN_SALT', '4+NR?-=*ykRsY~:mqj`5#9!=hRfSi<#&');
define('NONCE_SALT', 'sF=0{^1Y&WxIyd1Zc{*7&V~v+y6j0V0A');

Then, in your bbPress bb-config.php file...


define('BB_AUTH_KEY', '/xhuN*9-eSK//l}W-a=21z*+i6iz@N-6kWlTah[{gh}7{`%T~&B-HrYr5pgP+OxN');
define('BB_SECURE_AUTH_KEY', 'l9jwMX2U*+:sSzQo~8OOt}!54*1KV@:}-*');
define('BB_LOGGED_IN_KEY', '!]-^))}7PB_IZ+kzZV?H@sXvRC[K?:![lsd<6=4;UMKL_%Il%-axHv4*[g20m*g^');
define('BB_NONCE_KEY', '!.OW{67^@=BM-THdr`2i<2qH,t_|/7$|-nUw4+os8n@*[ZF/nnHc`c%vsI~hM?2#');
define('BB_AUTH_SALT', 'Jiz70.vxul~rpgfA#ll?:t24rF define('BB_SECURE_AUTH_SALT', 'Jf7,nC+C,*&!cX^~T#&Y6L,)1`%6y%+++4x}HLFX6M^*GYrDJ4ih?x-vW#NnJ?$T');
define('BB_LOGGED_IN_SALT', '4+NR?-=*ykRsY~:mqj`5#9!=hRfSi<#&C%nLS&u^p/JuPar)#Uj(j91JG|ThKjfY');
define('BB_NONCE_SALT', 'sF=0{^1Y&WxIyd1Zc{*7&V~v+y6j0V0A}#R==J,-Ql8t[$+ :jV-t5PR~xA+[M%U');

do the same thing - shorthen the 64-character strings down to 32-characters...


define('BB_AUTH_KEY', '/xhuN*9-eSK//l}W-a=21z*+i6iz@N-6');
define('BB_SECURE_AUTH_KEY', 'l9jwMX2U*+:sSzQo~8OOt}!54*1KV@:<');
define('BB_LOGGED_IN_KEY', '!]-^))}7PB_IZ+kzZV?H@sXvRC[K?:![');
define('BB_NONCE_KEY', '!.OW{67^@=BM-THdr`2i<2qH,t_|/7$|');
define('BB_AUTH_SALT', 'Jiz70.vxul~rpgfA#ll?:t24rF define('BB_SECURE_AUTH_SALT', 'Jf7,nC+C,*&!cX^~T#&Y6L,)1`%6y%++');
define('BB_LOGGED_IN_SALT', '4+NR?-=*ykRsY~:mqj`5#9!=hRfSi<#&');
define('BB_NONCE_SALT', 'sF=0{^1Y&WxIyd1Zc{*7&V~v+y6j0V0A');

 
* Note that these keys are exactly the same - the only bits different in the code are the ones in the bb-config.php file have “BB_” at the beginning of each of the key definitions.

 

If you need to generate new key codes, use the url provided in the WordPress config.php file...
https://api.wordpress.org/secret-key/1.1/salt/

 

Two final notes...

I know this has something to do with some specific server environments (obviously) because it works just fine in one place and doesn't in another. I'd love it if someone could offer up an explanation in the comments as to WHY exactly this happens and what server environmental variables cause this. My environment where this DID NOT work with 64-character keys was on a Media Temple (gs) GridService.

Lastly, I'd like to thank the great contributors on this page on the bbPress forums which ultimately led me to the solution to this problem (which I also saw mentioned on a few other pages that i didn't remember to bookmark).

capitalize page titles in WordPress

(without using a Plugin)

 

While working on a custom WordPress theme for a client this morning, I came across a peculiar problem. The words I was using for custom tags and categories which (in this particular theme design) get displayed in the page’s <title> in breadcrumb-like fashion, were being displayed just as they were typed in – with all lower-case characters. After doing some Google searching, I discovered there was no documented easy solution to capitalizing these text values. I found that there were many Plugins available that do this, but I really felt like installing a Plugin just to get some words capitalized seemed like overkill. Eventually, by using some of the code I found in those plugins, I hand-rolled the simple solution I was looking for, and thought I’d share it with y’all here…

First, let me explain that I did not have an issue with getting some things capitalized, like category name, for instance. To do that i simply used the PHP string function, ucwords() inline like so…

<title>Site Name - <?php echo ucwords($category); ?></title>

The problem I was having was with the WordPress template tags: wp_title and the_title. You can’t use ucwords() directly on these because they’re not a string. So, the solution I came up with was to write a small PHP function at the top of my “header.php” file that takes the text values from wp_title and the_title, turns them into a string, and applies capitalization with the ucwords() function…

<?php
function captitle($title) {
     $title = ucwords($title);
     return $title;
}
add_filter('wp_title', 'captitle');
add_filter('the_title', 'captitle');
?>

 
enjoy.

 

     
  • need my help?

    Interested in having me be a consultant on your next project?  to email me.

  •  
  • what i’m reading...

  • Run Windows on Mac OS X with no reboot!ING DIRECT Orange Savings Account- Apply Today!
    BasecampGo Daddy $6.99.com sale 125x125

    Order DIRECTV Today for $29.99 a month