WordPress the_title Filter Hook Generically and Semi-uniquely

WordPress the_title Filter Hook

I ran into a situation where I needed to apply a callback function to the_title filter hook in WordPress. I thought this would be a simple task. After creating my callback function and assigning it to the the_title filter hook, I thought it did exactly what I needed it to do. Except, then I noticed it was also applying the same filter to navigation items as well. Thus, breaking the layout.

How can I target the the_title filter hook while ignoring the filter effect for navigation items?

I was working on a section in a WordPress theme which I wanted to add buttons to the page title within the heading one tag.

It looked similar to this:

I was then “hooking in” like this:

However, the above targets literally everything which calls the_title filter hook, and this includes navigation items.

I changed the filter hook definition like this:

Pretty much every call to the_title filter passes parameter 1 as the $post->post_title and parameter 2 as the $post->ID. Search the WordPress core code for apply_filters( 'the_title'* and you’ll see for yourself.

So I decided to add a third parameter for situations where I want to target specific items which call the_title filter. This way, I can still receive the benefit of all callbacks which apply to the_title filter hook by default, while also having the ability to semi-uniquely target items that use the_title filter hook with the third parameter.

It’s a simple boolean parameter:

Label the variables however you want. This is what worked for me, and it does exactly what I need it to do.

Hope this helps someone in a similar situation.

Credits

Original question asked by @softsdev on stackoverflow: Why the_title() filter is also applied in menu title?
Answer to Question: Why the_title() filter is also applied in menu title? by @Michael Ecklund on stackoverflow.

Related Questions

This article assists in answering all of the following related questions:

  1. [stackoverflow] Apply the_title filter to post titles AND backend auto social-sharing plugin, but not nav menu asked by: @Andre Bulatov
  2. [WordPress Development] Apply the_title filter to post titles AND backend auto social-sharing plugin, but not nav menu asked by: @Andre Bulatov
  3. [WordPress Development] How to append to title via functions.php for auto-posting plugin  asked by: @Andre Bulatov
  4. [WordPress Development] Alter only the page title, not the post titles within asked by: @Mayeenul Islam
  5. [WordPress Development] Changing document title only on a custom page template asked by: @V Neal
  6. [WordPress Development] How to append text to title of Custom Post Type post (without affecting all titles on page)? asked by: @k998
  7. [WordPress Development] filter the_title problem in nav asked by: @greenbandit

WordPress Template for Post Type with Nested Taxonomy

WordPress Template Hierarchy

This article assumes you have a decent understanding of: the WordPress Template Hierarchy, Registering Custom Post Types, Registering Custom Taxonomies, and attaching Taxonomies to Post Types.

Let’s say you have a Custom Post Type of “Portfolio”, with a rewrite slug of portfolio. So the URL on the frontend is: domain.com/portfolio.

Let’s also say you have a Custom Taxonomy of “Categories”, with a rewrite slug of portfolio/category (attached to your Custom Post Type of “Portfolio”). So the URL on the frontend is: domain.com/portfolio/category/<term-slug>/

With the standard WordPress Template Structure and functionality…

  1. Upon visiting: domain.com/portfolio/ the Post Type template will display just fine.
  2. Upon visiting: domain.com/portfolio/category/<term-slug>/ the Taxonomy Term Template displays just fine.
  3. Upon visiting: domain.com/portfolio/category/ the Taxonomy template doesn’t work. Resulting in a 404 Not Found error.

I’m a believer in all “paths” in the URL structure should resolve to a valid page with relevant content.

  • Referring to #1 above, that can be handled in archive-$posttype.php in the WordPress Template Hierarchy.
  • Referring to #2 above, that can be handled in taxonomy.php in the WordPress Template Hierarchy.
  • Referring to #3 above, that’s not handled at all. There’s no template file in the WordPress Template Hierarchy to handle a “Taxonomy view”.

taxonomy.php in the WordPress Template Hierarchy refers more to the currently viewed “Term” within the “Taxonomy”. Which to me… Is kind of misleading.

So I’ve come up with a few tweaks which allow your WordPress theme to have a term.php AND taxonomy.php in the template structure of your WordPress theme.

  • term.php will handle the currently viewed Term within a Taxonomy (This is basically replacing the default functionality of taxonomy.php template file.).
  • taxonomy.php will handle the currently viewed Taxonomy (Basically redefining it’s default intended purpose.).

This will allow you to have a template for the Taxonomy itself, and another template for Terms in a Taxonomy.

Disclaimer:

I believe I have come up with a temporary solution from random miscellaneous sources online.

I would love for someone to expand on my answer to make it more “bullet proof” so to speak. However, in the short run, this solution DOES work.


Separate WordPress Template Files For Taxonomy Archives and Taxonomy Term Archives

Step 1 – Modify the Current Rewrite Rules Used by WordPress

1.1) We will be adding new rewrite rules to our existing rewrite rules currently being used by WordPress.

 

1.2) Generate new rewrite rules… (I think this area needs some improvement for sure — I’m not savvy with rewrite rules.)

There’s really only one rule that I found is absolutely necessary to make this work. I could be missing other important rewrite rules, but this is the one NECESSARY rewrite rule to make this adjustment to the template structure.

By adding the new rewrite rule, WordPress no longer sees domain.com/portfolio/category/ as a 404, but as a Post Type Archive instead. There’s more work to be done. We can’t leave this URL as a Post Type Archive, because then we will have a conflicting template structure with domain.com/portfolio/ and will result in unexpected behavior.

Step 2 – Modify the Post Type Archive Template File

We need to modify the behavior of the Post Type archive template file to conditionally load the Taxonomy template file instead. This is what’s triggered on domain.com/portfolio/category/

Now if you go to domain.com/portfolio/category/ it loads taxonomy.php from your currently active WordPress theme directory.

Step 3 – Modify the Taxonomy Template File

We need to modify the behavior of the Taxonomy archive template file to conditionally load the Term archive template file instead. This is what’s triggered on domain.com/portfolio/category/<term-slug>/

Now if you go to domain.com/portfolio/category/<term-slug>/ it loads term.php from your currently active WordPress theme directory.

 


Conclusion

  1. When viewing domain.com/portfolio/ – Default archive templates are loaded as usual. (archive.php, archive-$posttype.php, etc) from your currently active WordPress theme.
  2. When viewing domain.com/portfolio/category/ – The taxonomy.php template file is loaded from your currently active WordPress theme.
  3. When viewing domain.com/portfolio/category/<term-slug> – The term.php template file is loaded from your currently active WordPress theme.

You can easily test to see if this works. All you need is three files in your currently active WordPress theme directory.

  1. archive.php or archive-posttype.php
  2. taxonomy.php
  3. term.php

Put this in archive.php or archive-$posttype.php

Put this in taxonomy.php

Put this in term.php

 


Credits

Original question asked by @Michael Ecklund on WordPress Stack Exchange: Custom Post Type with Nested Taxonomy and Template Files.
Answer to Question: Custom Post Type with Nested Taxonomy and Template Files by @Michael Ecklund on WordPress Stack Exchange.

WordPress Stack Exchange

If you have a question related to WordPress, you can asked it on WordPress Stack Exchange and someone will very likely provide an answer or solution to your question or issue.

Related Questions

This article assists in answering all of the following related questions:

  1. [WordPress Development] Custom Post Type with Nested Taxonomy and Template Files asked by: @Michael Ecklund
  2. [WordPress Development] How to modify URL structures in custom post types and taxonomies or terms asked by: @Michael Ecklund
  3. [WordPress Development] Custom post types and permalink asked by: @theplayer777
  4. [WordPress Development] How to change permalink structure for custom post type and it’s taxonomies? asked by: @Mark
  5. [WordPress Development] How to show custom taxonomy in the permalink? asked by: @Lou
  6. [WordPress Development] Change Permalink Structure for Custom Taxonomy asked by: @Saurabh Goel
  7. [WordPress Development] Create permalink structure for one taxonomy archive per custom post type asked by: @jpsingleton
  8. [WordPress Development] Custom Permalink Structure for Custom Post Type, Custom Taxonomy, Under a page asked by: @61Pixels
  9. [WordPress Development] Custom permalink with child taxonomy terms asked by: @Shane
  10. [WordPress Development] Custom Permalinks for Custom Taxonomies/Post Types -> Top Level Pages not working asked by: @luke
  11. [WordPress Development] Custom Post Type and Custom Taxonomy Permalinks asked by: @Rynoceris
  12. [WordPress Development] Custom post type and taxonomy permalinks – Structure asked by: @Lucas Yarid
  13. [WordPress Development] custom post type archive template per custom taxonomy term asked by: @gfaw
  14. [WordPress Development] Custom post type taxonomies URL rewrite asked by: @Jaquis
  15. [WordPress Development] Custom Post Type with two hierarchical Custom Taxonomies: strategy to generate best permalink structure asked by: @Lopinsjk
  16. [WordPress Development] Custom post type, permalinks, taxonomies and blog posts asked by: @T G
  17. [WordPress Development] Taxonomy, Terms, and Template Files asked by: @ariefbayu
  18. [WordPress Development] Custom post types, taxonomies, and permalinks asked by: @RodeoRamsey
  19. [WordPress Development] Which template name to use for taxonomies but not their terms? asked by: @Maikal
  20. [WordPress Development] Custom taxonomy returns 404 asked by: @mihajloWR
  21. [WordPress Development] Custom Taxonomy specific to a Custom Post type asked by: @Saurabh Goel
  22. [WordPress Development] Define permalinks for custom post type by taxonomy in WP 3.4 by: @reekogi
  23. [WordPress Development] How to create a permalink structure with custom taxonomies and custom post types like base-name/parent-tax/child-tax/custom-post-type-name asked by: @Jeff
  24. [WordPress Development] How to setup a Custom Taxonomy Term template asked by: @NM6
  25. [WordPress Development] Need some templating advice for templating with custom post types and taxonomies asked by: @user3905
  26. [WordPress Development] permalink /category/post-name with custom post type and taxonomy asked by: @Owl
  27. [WordPress Development] Permalink structure CPT with custom taxonomy fails asked by: @NielsPilon
  28. [WordPress Development] Rewriting a custom-post-type permalink with taxonomy term? asked by: @mathiregister
  29. [WordPress Development] Shouldn’t this be easy?! Custom post type/custom taxonomy permalink asked by: @fxfuture
  30. [WordPress Development] taxonomy term names in custom post type permalink structure asked by: @wppatrickk
  31. [WordPress Development] Template files for Custom Post Type and Taxonomy asked by: @Paul D
  32. [WordPress Development] Template for Custom Post Type and its Category asked by: @Seyed
  33. [WordPress Development] Templates for Custom Post Types and Custom Taxonomies asked by: @aifrim
  34. [WordPress Development] Permalink structure between 2 custom post types and a taxonomy asked by: @Max Lutz
  35. [WordPress Development] WordPress Custom Permalink Structure with Custom Post Type and Taxonomies asked by: @gleenk

 

Adding Post Types and Taxonomies to Custom Admin Menu Pages

WordPress Admin Menu

Customizing the WordPress admin menu is something you’ll eventually need to or want to do at some point. Customizing admin menus with Post Types is pretty easy compared to Taxonomies.

When you create Custom Post Types and Custom Taxonomies in WordPress using register_post_type(); and register_taxonomy(); there’s a parameter for each function called show_in_menu.

If you look at register_post_type(); the parameter accepts a simple true or false but also accepts a string as an argument for show_in_menu:

show_in_menu
(boolean or string) (optional) Where to show the post type in the admin menu. show_ui must be true.

Default: value of show_ui argument

‘false’ – do not display in the admin menu
‘true’ – display as a top level menu
‘some string’ – If an existing top level page such as 'tools.php' or 'edit.php?post_type=page', the post type will be placed as a sub menu of that.

Note: When using ‘some string’ to show as a submenu of a menu page created by a plugin, this item will become the first submenu item, and replace the location of the top-level link. If this isn’t desired, the plugin that creates the menu page needs to set the add_action priority for admin_menu to 9 or lower.

Note: As this one inherits its value from show_ui, which inherits its value from public, it seems to be the most reliable property to determine, if a post type is meant to be publicly useable. At least this works for _builtin post types and only gives back post and page.

If you look at register_taxonomy(); the parameter accepts a simple true or false but DOESN’T accept a string as an argument for show_in_menu like register_post_type(); does:

show_in_menu
(boolean) (optional) Where to show the taxonomy in the admin menu. show_ui must be true.
Default: value of show_ui argument

‘false’ – do not display in the admin menu
‘true’ – show as a submenu of associated object types

Adding a Custom Post Type to a custom admin menu is simple. Just specify the slug of the custom admin menu as an argument for the show_in_menu parameter while using register_post_type();.

How about adding a Custom Taxonomy to a custom admin menu? That’s a bit tricky to do.

What I’m about to show you works for both Custom Post Types AND Custom Taxonomies. So do with it what you please.

Step 1 – Hook into init and register Custom Post Types and Custom Taxonomies.

 

Step 2 – Hook into admin_menu to create a custom parent admin menu, and add Custom Submenu Admin Pages, Custom Post Type pages, and Custom Taxonomy Pages all to the custom parent admin menu.

 

Step 3 – Hook into parent_file to correctly highlight your Custom Post Type and Custom Taxonomy submenu items with your custom parent menu/page.

If you need any clarification about how any of this works, read the following pages from top to bottom:

  1. Adding Custom Parent Admin Menus
  2. Adding Custom Child Admin Menus
  3. Roles and Capabilities in WordPress
  4. Registering Custom Post Types
  5. Registering Custom Taxonomies
  6. WordPress Plugin API :: Action Reference
  7. WordPress Plugin API :: Action Reference :: init
  8. WordPress Plugin API :: Action Reference :: admin_menu
  9. WordPress Plugin API :: Filter Reference
  10. List of All WordPress Hooks (including actions and filters)

Credits

Original question asked by @numediaweb on WordPress Stack Exchange: Show custom taxonomy inside custom menu.
Answer to Question: Show custom taxonomy inside custom menu by @Michael Ecklund on WordPress Stack Exchange.

Related Questions

This article assists in answering all of the following related questions:

  1. [WordPress Development] Show custom taxonomy inside custom menu asked by: @numediaweb
  2. [WordPress Development] How do I create multiple post types in same menu section in WP-admin? asked by: @Peter Westerlund
  3. [WordPress Development] Show custom taxonomies in admin panel under custom post type asked by: @Eoghan OLoughlin
  4. [WordPress Development] How to add a taxonomy into admin menu asked by: @genuy11512

Widget Ready WordPress Template Files Displayed Conditionally

Widget Ready WordPress Template Files Displayed Conditionally

As a WordPress developer, part of my concern for my WordPress users is the ability for them to change their WordPress website content as much as possible; Without having to resort to contacting me to do so for them. This makes me think outside of the box on how I can make the WordPress theme have an intended layout, while allowing the flexibility of dynamic content. That’s where widgets come into play!

WordPress Widgets provide a structured way to allow dynamic content in a specific area of your theme without (or at least minimize the chance of) breaking the layout.

I really think widgets are the wave of the future with WordPress theme development. Especially with all the fancy updates to the WordPress theme customizer.

If you’re a WordPress theme developer and you’re theme’s aren’t “widget ready”, you should really change that. You can start by reading the rest of this article. 🙂

The following “How to Conditionally Display Widget Ready WordPress Template Files Tutorial” is very basic and bare-bones, but enough to give you the idea to run with.

Disclaimer: This “how to / tutorial” doesn’t cover WordPress theme options. This assumes you already have your WordPress theme settings managed in some way.

In this “how to / tutorial” I’ll demonstrate how you can give your WordPress users more control over the header section of their WordPress website. Although, don’t stop your creativity there. You could apply this basic idea in many other conventions.

Step 1 – Tell WordPress About the “Widget Areas” Supported by Your WordPress Theme.

The following code should be pasted in your currently active WordPress theme’s functions.php file:

Step 2 – Create the WordPress Template Files for Each Type of Header.

In this example, you’ll want to create 4 new template files in the root directory of your currently active WordPress theme:

  1. header-column-1.php
  2. header-column-2.php
  3. header-column-3.php
  4. header-column-4.php

Step 3 – Make Each New WordPress Header Template File “Widget Ready”.

In this example, I’ll demonstrate a 4 column header template (header-column-4.php):

Step 4 – Edit header.php in Your Currently Active WordPress Theme to Activate Dynamic Header Output.

There you have it! You can now “dynamically” display content in your header template file using widgets. Optionally, you could display static content which could serve as you “default content” or “fallback content” in-case there’s no widgets assigned to that particular widget area.

This small and simple change can really add a lot of flexibility to your WordPress theme.

Credits

Original question asked by @AlonsoF1 on WordPress Stack Exchange: Show/hide widgets in WordPress admin based on current Advanced Custom Fields option.
Answer to Question: Show/hide widgets in WordPress admin based on current Advanced Custom Fields option by @Michael Ecklund on WordPress Stack Exchange.

Related Questions

This article assists in answering all of the following related questions:

  1. [WordPress Development] Show/hide widgets in WordPress admin based on current Advanced Custom Fields option asked by: @AlonsoF1
  2. [WordPress Development] What’s the best way to handle css for multiple header layouts/multiple custom header.php files? asked by: @AlonsoF1
  3. [WordPress Development] How Can I Register Menus and Widgets Conditionally Based on Theme Options/Settings? asked by: @Ryan Dorn
  4. [WordPress Development] Custom Menus, Widgets & Conditional Statements asked by: @Justice Is Cheap

Numerical WordPress Pagination w/ Archives & Custom Queries

Numerical WordPress Pagination

I came across an interesting question (Pagination doesn’t work) that just happened to “catch my eye” on the WordPress Stack Exchange, regarding a numerical WordPress pagination.

In the question asked by @Paul I noticed he made use of the WP_Query class to perform a custom query in order to fetch posts from a Custom Post Type.

That’s totally fine and actually very practical / common when dealing with various WordPress archives.

The problem when you create a custom query is: now you can’t use a lot of the default functionality as you normally would have; You need to make some adjustments, in order to get the WordPress pagination to work properly.

Most of the WordPress functionality has to do with the default query which is performed on every page load. When you create a custom query using WP_Query, you’re not replacing the default query with your custom query. You’re adding a SECOND query (your custom query) to run parallel with the FIRST query (the default query).

Most (if not all of the WordPress functionality) expects the default query, but can be customized in-order to produce the desired result.

It would be best to only have ONE query instead of TWO queries for the requested page, especially if they’re querying the same source.

You can customize the default query several different ways, but the most common way would be to hook into the pre_get_posts action and modify the query before the query is performed. There are also more complex ways of modifying the query, for example, modifying the SQL using the filter hook posts_request.

This can be a tricky task and if not careful can actually cause unexpected behavior in other areas throughout your WordPress website.

This is why WordPress developers often choose to just create a brand new custom query using WP_Query to retrieve exactly what they want, without the worry of interfering with other areas of their WordPress website.

So in this article I’ll be demonstrating how to create a numerical WordPress pagination to add to your WordPress archive pages, when the posts have been fetched using a custom query via WP_Query instead of using the default query available by WordPress.

To begin, you’ll need to start by setting up your custom query. Your query might look something along the lines of:

If you want your numerical WordPress pagination to work with your custom query, then you need to be aware of the current page the request is performed on.

The current page number is actually referred to as paged. You can determine the current page number using some code like this:

Once the current page has been determined, you can setup your custom query to look similar to this:

Once the query has been performed, you then need to check if it’s located any posts in the database which match your criteria:

Finally! Now for the numerical pagination! I recommend using a WordPress function called paginate_links() for this part.

I don’t really understand the really unlikely integer part for the base, but that’s what’s demonstrated on the WordPress Codex page linked above.

  • You need to specify the current page (paged) — once again.
  • You need to know the total number of posts (which can be obtained from the $query variable object).
  • You need to specify a return type for this function. (Personally, I like it to be in array format, so that I can customize the pagination however I see fit).

This code needs to be placed within the condition which states that we have posts available. Not inside the loop, but immediately AFTER the loop.

Once the numerical pagination has been configured, it’s ready for output (or ready to be displayed). This is where paginated items are returned in array format.

I like to wrap it with a conditional check and then loop through the items performing additional conditional checks along the way.

 

Note: I use Bootstrap for my WordPress themes. So if you want your pagination to look awesome, with minimal effort involved. Try out Bootstrap.

To conclude everything mentioned above, here’s a all-in-one copy/paste starter solution for anyone looking to create a numerical pagination with a custom posts query using WP_Query.

 

Credits

Related Questions

This article assists in answering all of the following related questions:

  1. [WordPress Development] Pagination doesn’t work asked by: @Paul
  2. [WordPress Development] Pagination not working with custom loop asked by: @nurain
  3. [WordPress Development] WordPress custom post type category pagination 404 Error asked by: @21zna9
  4. [WordPress Development] The Pagination doesn’t work asked by: @hazelnut
  5. [WordPress Development] Pagination doesn’t work in custom page template asked by: @dev-jim
  6. [WordPress Development] Custom pagination code is not working asked by: @strange man
  7. [WordPress Development] 404 – Category pagination for custom post type asked by: @waffl
  8. [WordPress Development] Pagination of custom post type leads to 404 error asked by: @Benmay
  9. [WordPress Development] The pagination on index.php doesn’t work on a first page asked by: @Sergei Rodionov
  10. [WordPress Development] Permalinks not working when using a custom query for a custom post type asked by: @Leon
  11. [WordPress Development] Pagination not working it makes no sense asked by: @Grégoire Llorca