scriptygoddess

I can't speak for anyone else who has run into this problem, but I know I have run into it on more than one occasion. Having just figured out my particular issue this time, I'm making a note for myself (and anyone else who may have the same problem).

You may run into this problem if you have a template that you have "hardcoded" the header (in my case – it was a custom page template that actually needed a completely different header than the rest of the site – so I opted not to use "get_header()" on this template and just put the custom header right there in my custom page template…)

I *did* remember to include the "wp_head()" – so that wasn't the issue (although I have fogotten to do that on other occasions and that will cause all kinds of trouble, including seo plugin stuff not working) – but in this case – the other meta tags were coming in – but the titles weren't being rewritten.

The problem was the fact that I wasn't using get_header() – this must be the function that kicks off the search for that title tag and replaces it with the rewritten titles. If you don't use get_header() – your titles will not be rewritten.

You will run into the same problem even if you don't "hardcode" your header – and use the "include" line instead:

include( TEMPLATEPATH . '/header2.php' );

Whatever happens with "get_header()" – you need to run it to get those titles working.

(I've since updated this post – scroll down to the bottom for the simplest solution!!)
So if you have a different header for your page template – what you need to do is within your header.php – determine which header file it should load in… but ONLY AFTER you have the title tags written out… You could even use the "is_page_template()" function to figure out which header to pull in.

So my header.php looked something like this for this project I was running into with this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?php wp_title('«', true, 'right'); ?> <?php bloginfo('name'); ?></title>
<?php
if (is_page_template('my-custom-page-template1.php') || is_page_template('my-custom-page-template2.php')) {
include( TEMPLATEPATH . '/my-custom-page-template-header.php' );
} else {
?>
... rest of the header is here...
<?php } // endif custom page template checking... ?>

Now you can put your custom header in that "my-custom-page-template-header.php" (or whatever you want to call it) and on your "my-custom-page-template1.php and my-custom-page-template2.php, etc just use get_header() at the top like normal.

Actually – here's an even better/simpler option:

Name your custom header like this:
header-my-customheader.php.

Then when you call "get_header()" – do so like this:

get_header('my-customheader');

When using WordPress as a CMS, I often repurpose the "posts" as other types of content a site may need. For example, news or press releases, or any information that might need the ability to be catgorized and/or tagged (like testimonials). However, this may mean that you need a completely different template for your news page than you would want for your testimonials page.

WordPress gives you the ability to create category specific templates. So let's say your "news" category has an ID of 5 – if you create a category.php page to be used by all the other categories, and also created a category-5.php page – then just your news page will use that template for the category archive page.

But – what if you have a huge handful of subcategories and you want them all to use the same category template as it's parent? (Let's also assume that we can't just make this the default template) Here's what I came up with. Let's say that Testimonials is our category – it has an id of 12. It also has a dozen or so subcategories (for argument's sake, lets say we've sorted our testimonials into groups from what types of companies these testimoinals have come from "Consulting Firms", "Web Host Providers", "Design Firms", etc.

We don't want to duplicate our category template for every subcategory we have. That's a nightmare to manage. So lets do this instead: In your category.php template file – before ANYTHING ELSE – even before you call get_header() – we add the following:

$thecategory = get_category($cat);
if ($thecategory->category_parent == '12' || $thecategory->cat_ID == '12') {
include(TEMPLATEPATH.'/testimonials-template.php');
} else {
/* include... default template file here like we included the "testimonials-template.php - or you can just wrap this around your actual template code... */
}

So what this does is it gets information about the category – if the current category's PARENT is 12 OR we are in fact looking at category 12 – then we pull in that special testimonials-template.php file… Otherwise the other code would be executed. (In the file I was using I just wrapped that around my existing category.php template code…

Please note: I have not tested this on if you have a SUB SUB category – I'm thinking $thecategory->category_parent probably only looks one level above the current category… so keep that in mind.

(I make the note above that this does not include a plugin because when I was trying to get this to work – one solution I had found online involved installing a plugin that kind of messed with my category heirarchy in a way I wasn't crazy about. Personally, I think this is a much simpler solution)

13 Jan, 2010

Never Update the Copyright Year Again (on a PHP page)

Posted by: Jennifer In: PHP

This is a really silly little trick – but if you have a PHP page that has a © copyright year at the bottom – there is no reason you should be updating that every year (unless it's just something you overlooked initially) :) As you get requests from clients now that we're in a new year to change the copyright year in the footer – do yourself a favor and use this instead so you never have to do it again. :)

<?php echo date("Y"); ?>

Ran into a bizarre problem today using swfobject. A lot of wasted time, but I'll give you the short story / solution.

I'm not sure of what other factors played a role (the fact that the element was in a absolute positioned container, or that the immediate container to the flash element was in a float) but I had the call to the swfobject javascript in the BODY tags of the html (not within the head tags). This was apparently causing Firefox to not display the flash. Simply moving the swfobject javascript code within the head tags of the html instantly fixed the problem.

I know I've used swfobject inside the body before – so I'm sure there's something else that contributed to the problem.

On a site I was working on recently, the client was using the Featured Content Galleries plugin for their homepage (you can download it from here) – but they wanted to add a gallery like this to other pages on their website and have other featured content on those pages. This is not a feature currently supported "out of the box" with this plugin. But hacking it in is pretty easy.

First – you will need to create a category that you will use for the other Featured Content Galleries – one per page you want to use it with. Assign posts you want to appear in the featured content gallery with this new category. For example – lets say on your homepage you're running the featured content gallery, and it's showing posts from the "featured-posts" category. But lets say you have a "products" page and on that page, you only want articles related to your products showing up on that page. So you would need to create another category – lets call it "featured-posts-about-products". Then assign this new category to your articles about products. (**Make a note of what the category ID is of this new category!)

You will still need to make sure those posts have a custom field with a key of "articleimg" and a value that is the full URL to the image you want in the featured content gallery.

Now we can start hacking at the plugin…

Open up gallery.php (found in the featured-content-gallery plugin directory). MAKE A BACKUP OF GALLERY.PHP JUST IN CASE! :) Then, look for this line:

query_posts('category_name=' . get_option('gallery-category') . '&showposts=' . get_option('gallery-items'));

and replace with the following:

$categoryToUse = 'category_name=' . get_option('gallery-category');
if (isset($catidforgallery) && is_numeric($catidforgallery)) {
$categoryToUse = 'cat=' . $catidforgallery;
}
query_posts($categoryToUse . '&showposts=' . get_option('gallery-items'));

NOW in your theme – where you have the featured content gallery appearing – if you want it to pull from that special category, you can do this:

<?php
$catidforgallery = 1234;
include (ABSPATH . '/wp-content/plugins/featured-content-gallery/gallery.php');
?>

The above code assumes our category ID is "1234".

But lets say you're using this template for a number of different pages, and on each one you want a different content gallery… Again, set up these new categories, make notes of what their IDs are – then on each page that's using this template, to specify what category ID the featured content gallery should use, add a custom field with a key of "category-feature-rotator" and a value of the category ID you want in the gallery.

Modify the theme/template code to this instead:

<?php
$category_feature_rotator = get_post_meta($post->ID, "category-feature-rotator", true);
$catidforgallery = false;
if (isset($category_feature_rotator) && $category_feature_rotator != 0) {
$catidforgallery = $category_feature_rotator;
}
include (ABSPATH . '/wp-content/plugins/featured-content-gallery/gallery.php');
?>

That will look for the custom field and use the category ID you put in for that custom field – and if it's not there – then it will just default to the category specified on the Featured Content Gallery options page.

IMPORTANT NOTE: If/when there is an update to the Feature Content Gallery plugin – upgrading will overwrite these changes. This post was written for version 3.2.0 of the plugin. Hopefully they'll build this feature in a next release of the plugin. :D

UPDATE 11/16/09 Greg had asked in the comments about being able to do a similar technique with multiple featured content galleries, but being able to specify page IDs. He was able to solve the problem before I could and sent me the solution. :)

To use Page IDs in the gallery: On the page you want to have the gallery displayed on, create a custom field with a key of "pageids" – and for the value, give a comma seperated list of page ids.

Where I had been pulling the "category-feature-rotator" custom field – replace that with the following:

$pageids = get_post_meta($post->ID, "pageids", true);

Then look for the following line in gallery.php (should be around 24)

$arr = split(",",get_option('gallery-items-pages'));

and change it to this:

$arr = split(",",($pageids));

*Note: I haven't personally tested the Page ID method – so if you try it – please report back how it goes…

Background: For a site I was working on, I was pulling in content from another page onto the homepage. However I wanted to just bring in an excerpt not the whole page. (And actually, a customized excerpt at that – not a default set character or word limit). I also didn't want any images brought in – I just wanted the text and any associated formatting.

Well, one problem here is that WordPress pages (at least as of this writing – with WordPress in version 2.8.5) do not have excerpts for pages. Only for posts. The way around this is to use the <!-- more --> tag where you want your break. So that works fine – but what about stripping out the images? I'm still working on my reg-ex knowledge, but I found this one from here and it worked for me. So this is what I'm doing when I'm pulling in my page excerpt:

<?php
$posts = query_posts('page_id=1234');
if (have_posts()) : while (have_posts()) : the_post();
//this makes the more work...
global $more; $more = 0;
?>
<h1><?php the_title(); ?></h1>
<?php
$content = get_the_content('');
$content = preg_replace('/<img[^>]+>/is', '', $content);
echo $content;
?>
<p><a href="<?php the_permalink(1234); ?>"><img src="/images/more.gif" /></a></p>
<?php endwhile; endif; ?>

*we're assuming "1234" is the ID of my page in the example above…

You'll also notice I manually added my "more" button image…

To read more about the more tag (and see where I got that global $more; $more = 0; from, you can see the WordPress codex here on the subject. (Scroll to the very bottom of the page – to the section title "How to use Read More in Pages")

I just saw this plugin show up on the WordPress Plugins list. WOW! I haven't had a chance to use it yet, but can think of a dozen places where I can use this feature!!! Very excited to see it!

Secondary HTML Content – WordPress Plugin

07 Oct, 2009

Delink Pages Plugin

Posted by: Jennifer In: WordPress| WordPress Plugins

(Plugin and this post last updated: 10/20/2009 – Latest Plugin Version: 1.1.1 – see changelog at bottom of post)

I have been wanting to do more wordpress plugin development, but it has a somewhat steep learning curve. So I'm testing the waters a bit here. I've done some testing with the plugin, but feedback is appreciated. For now, if you have problems with the plugin, leave a comment here. PLEASE include contact information (fill out the email field with a valid email address!!), a URL where you have the plugin installed, any errors you're getting, etc. The more information you give me, the more likely I will able to fix the problem.

Plugin Description

This plugin will allow you to specify certain pages to not be linked when wp_list_pages() is used in your theme. You might want to do this if you want a header for a series of subpages, but don't specifically want that header to be a real "page" or link.

For example – you have a series of pages like this:

As of version 1.1 of this plugin there are two ways you can go about doing this. You can either remove the link entirely. As in no <a> tag at all like this:

OR you can remove the link to the actual page and sawp it out for a blank link to "#".

Usage

To use the plugin, download the zip, and unzip the file. Install it by putting the scripty-delinkpage.php in your wp-content/plugins/ directory. Activate it through the WordPress admin. Then, on pages you do not want to have a link, add a custom field with a key of "delink" and a value of "true" when you want to remove the link completely, or a value of "href" when you want the href value to be "#".

Download

You can download this plugin from wordpress.org plugin directory here: Delink Pages

Changelog

Version 1.1.1
Minor bug fix to preg_replace

Version 1.1
Added option for delink to have a value of "href" when you want don't want the link to be removed entirely but be linked to "#"

Version 1.0
Initial release

One of my clients had set it up so that one particular page in their WordPress install would load as https:// and even though they changed all links they could find in their template to use root-relative links, they were still getting complaints from IE about the page loading some secure and non-secure items. A look through the source code revealed that two plugins in particular (an event manager plugin and cforms plugin) pulled in their CSS or javascript files without the https. It probably got these settings from the main WordPress install, which wasn't set to use ALL https – the had just set it up so that this one page would be (using a special template for https pages). But there was no way to get that information to the plugin… So I needed to do a little hacking to the plugin(s) and add in a conditional statement that would check if the page being viewed was https – and if so, swap out http:// for https:// in the path to the file.

In the case of the cforms plugin, the file I had to modify was cforms.php – the line I found that pulled in the file path looks like this:

$cforms_root = $cformsSettings['global']['cforms_root'];

So, just below that I added this:

if(isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == "on") {
$cforms_root = str_replace("http://","https://",$cforms_root);
}

Pretty simple. Of course, if/when we need to upgrade the plugin, the change will be overwritten – but it's pretty easy to add back in…

(Apparently, I'm not the only one with this issue…)

In the case of the event manager plugin, the file I modified was dbem_events.php:

So just below this:

function dbem_general_css() {
$base_url = get_bloginfo('url');

I added this:

if(isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == "on") {
$base_url = str_replace("http://","https://",$base_url);
}

No more complaints from IE.

One problem with the WYSIWYG editor in WordPress is that if you are trying to manually add in a block of HTML, the editor may try to translate that block into paragraph format, inserting <p> tags where you didn't intend, possibly (and most likely) ruining what you were intending to do with your HTML code block.

Previously, my way around this had included the use of various plugins that had you wrap certain areas in your post with specific comments that would tell WordPress to leave that block alone and not insert those paragraph tags. There was still some issues doing it this way – as it required you to view these posts in the HTML view. Viewing them in the visual editor would mess everything up.

So, keeping in line with what seems to be a current trend at the moment of solving all my problems with shortcodes, that's how I've decided to solve this problem as of late.

First – (you'll only need to do this once) – add the PHP code so that you can do the shortcode. If you don't already have a functions.php file in your theme directory, create one and add the following code:

function scriptysAddHTML_func($atts) {
global $post;
$id = $atts['id'];
if (empty($id)) return;
return get_post_meta($post->ID, $id, true);
}
add_shortcode('html', 'scriptysAddHTML_func');

Then, in your post when you want to add some custom HTML, paste your block of HTML into the value of a new custom field. For the name of this custom field, give it a unique name with no spaces. (For example: My_HTML_Block)

In your WordPress post, where you want this block to appear, add the shortcode like this:

[html id=My_HTML_Block]

You would add it exactly like the above, except instead of My_HTML_Block, you would change that to be the name you gave your custom field.

Extra Tip If for some reason, you're using more than one shortcode in your post and it doesn't seem like they're working… if you're using them one right after the other, make sure there is a space between them. ie. If you're using my linebreak shortcode, and you were adding more than one linebreak… it might not work if you do this:

[br][br][br]

You my have to do this:

[br] [br] [br]

Little minor thing, but something you might not have thought of… figured I'd mention it. :)

Featured Sponsors


  • Michael: You can use get_header(2) in your case. The filename of your custom header has to be header-2.php. The problem with include(your_file.php) is, all
  • cliff: hi wonder if you can help me, pls i designed www.kouga.mobi using dreamweaver CS3 and now want to make the phonenumbers into links so that if you
  • jerey: how do i rewrite this because it tried RewriteEngine on #Options +FollowSymlinks RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FIL

About


Advertisements