Tag Archives: wordpress tips

Simple Google Adsense Code Insertion/Injection WordPress Plugin using Custom Fields/Meta

[iframe width=”100%” height=”349″ src=”http://www.youtube.com/embed/tKUq5TZFeCU”]

Today I was working on adding a Google AdSense Ad into a post on the site, and I couldn’t (easily) find a WordPress Plugin that would let me do that easily; so, as always, I wrote one.

[adsense metakey=”my-cool-adsense-ad”]

To use add the shortcode to your post. (Not here, because it would actually try and insert something, please see video on these steps.)

Put your adsense code (that you copy from Google Adsense) in a Custom Field and call it whatever you want (called adsense-ad-1 here). The shortcode will pull in the AdSense code from that Custom Field in the location you put the shortcode in your post.

Enjoy.

Simple Google AdSense Code Insertion Plugin [zip]

[php wraplines=”true”]
/*
Plugin Name: Simple Google Adsense Code Injector Shortcode
Plugin URI: https://devblog.aubreypwd.com/
Description: This easy shortcode will allow you to insert your adsense code easily into your posts. Just put your adsense code betweent <code>adsense /adsense</code>.
Version: 0.1
Author: Aubrey Island
Author Email: enethrie@gmail.com
Author URI: https://devblog.aubreypwd.com
*/

function adsense_hand_enji( $atts, $content = null ) {
global $post;
extract( shortcode_atts( array(
‘metakey’ => ”,
//’attr_2′ => ‘attribute 2 default’,
), $atts ) );

$code= get_post_meta($post->ID, $metakey, true);

return $code;

}

add_shortcode( ‘adsense’, ‘adsense_hand_enji’ );
[/php]

Change Sub Category to Main Category URL Structure (WordPress Remove Sub Category Hierarchy URL)

While working on Enjfi, I ran into an undesired feature. When browsing to a sub-category, in WordPress, I ended up with a URL structure like:

[sourcecode]

https://devblog.aubreypwd.com/category/sub-category

[/sourcecode]

Now, that usually works. But, with Enjifi, I may want to move sub-category under category-2, like:

[sourcecode]

http://enjifo.com/category-2/sub-category

[/sourcecode]

Now, all my links to /category/sub-category are not /category-2/sub-category, the undesired feature.

So, I found this, and I wrote a plugin for it (because it worked).

sub-cat-to-main-cat.zip

[php wraplines=”true”]
<?php

/*
Plugin Name: Change Sub Category to Main Category URL Structure (WordPress Remove Sub Category Hierarchy URL)
Plugin URI: https://devblog.aubreypwd.com/
Description: This plugin will tell your website to remap /category/category/subcategory to /category/subcategory
Version: 0.1
Author: Aubrey Island
Author Email: enethrie@gmail.com
Author URI: https://devblog.aubreypwd.com
*/

/* From: http://wordpress.stackexchange.com/questions/7807/changing-the-category-permalink-structure/7817#7817 */

add_filter( ‘category_link’, ‘wpse7807_category_link’, 10, 2 );
function wpse7807_category_link( $catlink, $category_id )
{
global $wp_rewrite;
$catlink = $wp_rewrite->get_category_permastruct();

if ( empty( $catlink ) ) {
$catlink = home_url(‘?cat=’ . $category_id);
} else {
$category = &get_category( $category_id );
$category_nicename = $category->slug;

$catlink = str_replace( ‘%category%’, $category_nicename, $catlink );
$catlink = home_url( user_trailingslashit( $catlink, ‘category’ ) );
}
return $catlink;
}

?>

[/php]

Allow HTML in your Category Descriptions in WordPress

So, while I was actually starting to move posts over to this lovely site, I noticed that when I saved a category’s description, certain HTML was being stripped.

For example (please no comments on quotes, this is HTML5!),

[html wraplines=”true”]
<a href=http://www.vim.org/ target=_blank>Vim</a> is an editor that I came across when looking for alternatives to, what I was currently using, <a href=http://notepad-plus-plus.org></a>Notepad++. I eventually started customizing it and trying to get it to work for me. At one point I even gave up, but came back to give it a try again.

It eventually lead to <a href=https://devblog.aubreypwd.com/category/vim/aubreys-gvim></a>Aubrey’s gVim, as well as <a href=/tag/vimtips>vimTips</a> and <a href=/tag/aubreygvimtips>Aubrey’s gVim Tips</a>.
[/html]

In this piece of code, inside a category description, though links were allowed, the class=it was not!

This plugin really came in handy!

[Allow HTML in Category Descriptions]

Problems Using WordPress update_option/get_option with PHP serialize?

Are you getting the error…

“unserialize() expects parameter 1 to be string”

…when using WordPress’ get_option function to get serialized data right after the file just saved serialized data using update_option? So was I using, basically, this same type of script:

<?php

$array[]="foo";
$array[]="bar";

update_option('foo',serialize($array));

//here is where I would get the error.
$data = unserialize(get_option('foo'));

?>

So, I had a problem. So, I tried encoding the string to try and speed up the saving time. Using PHP’ base64_(encode/decode) to encode/decode the serialized sting, it has been saving with no problems.

<?php
$array[]="foo";
$array[]="bar";

update_option('foo',base64_encode(serialize($array)));

//No more error?
$data = unserialize(base64_decode(get_option('foo')));
?>