post_class not including classes with hyphens - wordpress

I'm using the post_class function to add custom classes to my content in the content-page.php template.
I'm trying to add a class with a hyphen like this
post_class('class-name');
but it returns nothing on the div, whereas if I include a class without a hyphen it correctly adds it.
I've tried as using an array, and storing it in a variable but neither worked.
Has anyone ran into this issue?

Sounds like one of those typical WordPress bugs which have to do with hyphens, maximum characters etc. etc. Your may report this as a bug to the WordPress project.
The following will hopefully work (untested):
<?php
$classes = array(
'class-name'
);
?>
<div <?php post_class( $classes ); ?>>
Sometimes WordPress has strange behaviours. Have you tried
post_class('long-class-name');
and
post_class('alotofchars-classname');
It may work.
Also make sure that you tried with deactivating your Plugins - it may also be a Plugin problem.
And ofc you have to be inside the loop.

Adding post classes with hyphens works just fine (tested locally on the latest WordPress release).
The likely issue here is a plugin, or custom code modifying the post classes. The generated post classes, together with any you pass in to post_class() are passed through a WordPress filter. This gives other plugins / code outside of WordPress core the opportunity to modify the list of classes before they are used.
If you take a look at the code in your theme, or plugins you'll find something that is attaching to this filter and causing the issue you're seeing.
Take a look for anything that is calling add_filter() with 'post_class' as the first argument.

Related

Using Timber with the updated Calendar designs in The Events Calendar Plugin

I'm using the events calendar plugin for Wordpress with a Timber based theme. I'm currently using a previously documented method to integrate which is here:
https://theeventscalendar.com/support/forums/topic/events-pages-not-showing-up-in-timber-based-theme/
Since v5 of the plugin there is an option [set to default too] which uses the updated calendar and event designs that breaks my current integration method. I've tried to update my template PHP files but with little success.
The legacy functions which are relatively simple to call using the function() method in Timber are:
<?php tribe_events_before_html(); ?>
<?php tribe_get_view(); ?>
<?php tribe_events_after_html(); ?>
https://timber.github.io/docs/guides/functions/
However in the v2 templates added to The Events Calendar in v5 they are calling a class, e.g:
use Tribe\Events\Views\V2\Template_Bootstrap;
get_header();
echo tribe( Template_Bootstrap::class )->get_view_html();
get_footer();
The main problem I have is that I'm unsure how to expose this class to the Timber context. If anyone could give me a steer it would be much appreciated. What I have now works fine but the tribe_get_view function is deprecated and considered legacy so is not going to be around for ever so I need to come up with some sort of solution.
Thanks!
Right so the method I posted is no longer require at all and neither is a work around. Just make sure you have a page-plugin.twig template available. Hopefully this will help anyone else who ends up in a bit of a rabbit hole.....
To clarify, there is a workaround in timber/starter-theme, which involves multiple files, including a 'page-plugin.twig' template. Don't just add a 'page-plugin.twig' template and expect that to fix it.
Third party plugins that hijack the theme will call wp_head() to get
the header template. We use this to start our output buffer and render
into the view/page-plugin.twig template in footer.php
Go here https://github.com/timber/starter-theme.
Copy the header.php, footer.php, and page-plugin.twig into your theme and customize for your own site.
The method documented here: https://mcintosh.io/using-the-events-calendar-plugin-with-timber-and-twig/
Worked for me. You create a default template file and a matching twig template:
[your theme]/tribe/events/v2/default-template.php
[your theme]/[your twig template directory]/events.twig
The default-template.php file contains this line:
$context['tribe_markup'] = tribe( Template_Bootstrap::class )->get_view_html();
Which can be rendered like this in the events.twig template:
<div class="events-container">
{{tribe_markup}}
</div>
Note that you will need to have to select "Enable updated designs for all calendar views" in events > settings > display
I think the div.events-container may be needed for the default stylesheets, but haven't tested otherwise.

What is the right place for reusable render functions in WordPress theme?

Let's say I have a reusable peace of HTML used on different pages in my theme.
In order to not duplicate myself I want to put this peace of HTML into some place and reuse it later. And because it has some parameters (needed for rendering) - it should be a function.
It seems to me that I cannot use get_template_part function because I want to pass some parameters into the render function.
functions.php feels more like a place for low level theme functionality.
What is the right place for render functions in WordPress?
UPD I've just found that in _s theme that I am using as a base code for my theme they have inc/template-tags.php. It feels like a good place. Is it the right way?
check codex set_query_var and get_query_var
In main template
set_query_var('customer_name', $customer_name);
get_template_part( $slug, $name );
template part retrieve it as
$customer_name= get_query_var( 'customer_name', 1 );
echo $customer_name;
Ok so in _s theme they suggest:
Custom template tags in inc/template-tags.php that keep your templates clean and neat and prevent code duplication.
Seems like a good place was found :)

How do I target a SPECIFIC template with is_page_template() in Wordpress 3+

I'm running Wordpress 3.2 and I need a conditional to test for a certain template. My template is a file called special_offer.php. The name of the template is "Special Offer". I've tried both of the following:
is_page_template('special_offer.php');
is_page_template('Special Offer');
Neither of these work! The documentation clearly says that the parameter should be a string with the name of the file so I'm not sure what's wrong. I know the function is at least partially working because if I don't include a parameter, it returns true for any pages using templates (as expected).
AHA!
I solved this by adding wp_reset_query() just before the conditional.
I had already read warnings about many WP conditionals not working inside the loop. However, my conditional is NOT inside a loop. Turns out, it won't work even AFTER the loop, which is where mine was. So you you need to reset the query before you call it.
It's worth double checking which file is being used according to the WordPress template hierarchy [image].
Try adding this magic constant to the template file you think is being called:
<?php echo(__FILE__); ?>
If you don't see a path and filename a different file is being used (refer to the template hierarchy diagram in the link above) or check which templates are available:
<?php print_r( get_page_templates() ); ?>
(^ looks like this has to be called from the admin interface e.g. in a plugin)
Also, the documentation seems to state that is_page_template() won't work in the Loop without calling another function first.

Insert a plugin manually into wordpress page

I am working in worpress front page.
I want to add a plugin to the page at a specific location manually but adding the code to the page myself.
I basically want to include a plugin in a certain page on a certain location. So I'm create a div...
<div id="plugin-holder">
**Plugin-will-appear-here-with-this-code**
</div>
Don't anyone know how this is done please?
Thanks
If you're wanting a plugin to appear somewhere, you'll be looking for "shortcode" functionality.
This is actually surprisingly easy to code, check out the examples in the Codex under Shortcode API - ie:
function bartag_func( $atts ) {
// ... do more things here ...
return "text to replace shortcode";
}
add_shortcode( 'bartag', 'bartag_func' );
Once you've called these functions you can use [bartag] in code and it will run your function and replace the shortcode with the generated text your function returns.
If you're adding shortcode functionality to your site, it generally makes most sense to code a really simple plugin and put it in that. The reason why this works best is that, over time, it's really easy to forget and upgrade a theme by mistake (or even change to a new theme) and thus break your site by losing your custom code in your former functions.php. Surprisingly, this is pretty easy to achieve and only requires some specially formatted comments at the top of your plugin file and a little common sense in coding - there are many tutorials and "how to"s around!
Here's a useful shortcode tutorial: http://www.reallyeffective.co.uk/archives/2009/06/22/how-to-code-your-own-wordpress-shortcode-plugin-tutorial-part-1/
You should add the relevant plugin code to functions.php.
I suspect you'll want to use some conditional tags, like is_home() to pinpoint your location. But maybe not, depending on what you are trying to do,
Also, if you're trying to to insert from a pre-existing plug-in, make sure you remove the register_activation_hook or activate_pluginname action.
If your plugin supports a sidebar widget you can simply "widgitize" the div tag that you wish to insert the plugin into.. Google the term and you are gonna find many resources.

Wordpress: apply_filters & add_action to the_content = EVIL?

I asked this question over in the actual tutorial, but not sure I'll get an answer anytime soon as it's almost 2 months old... so I'll take a gander here...
Tutorial is here: Build a WordPress Plugin to Add Author Biographies to your Posts
To sum up the tutorial and what the problem is, The tutorial adds an Author Bio on to the end of the content like so (the short version):
function x($content) {
return $content . "Author Bio";
}
add_action('the_content','x');
The Problem:
When someone uses:
$z = apply_filters('the_content', 'some content here');
echo $z;
The Author Bio will end up applied to $z and if $z is echoed out in the middle of some page… the Author Bio would be in the middle of some page… correct? (it's correct because I've tested it...)
Is there a better way to apply something to the end/under/below the_content hook? other than add_action(‘the_content’, ‘some_function’) because this to me seems evil...
or is apply_filters(‘the_content’, ‘some content here’) not the norm or something developers shouldn't be using inside their WordPress templates…? (which seems pretty much the norm, at least upon Google-ing formatting "the_content" outside the loop)...
Using apply_filters('the_content','some content here'), while it may not be 'the norm' (I don't know. I haven't seen it before, but if I needed formatted text, that's what I'd do), is a perfectly valid use of filters to get some text formatted like the content. Unfortunately, there's no better way to append something to content from a plugin. That is just the way these things work.
However, there's a (less than optimal) way of circumventing this. As part of the setup/install process for your plugin, have the user insert a custom function call, action, or filter into their theme. I know several plugins that do this, so it's not all that uncommon. Something like this:
do_action('my_super_awesome_bio_hook');
Would allow you to hook in without worrying about adding a bio to unexpected (and unintended) content. Even better would be inserting a filter:
echo apply_filters('my_super_awesome_bio_filter_hook','');
That would allow your plugin to modify the bio, but also allow the one using the plugin to override it if necessary (like on pages where they're just using excerpts, like search results, etc.).
Hope this helped.
Also, one minor addendum: you should be using add_filter, not add_action to append the Author Bio. add_action still works, but it's a filter you want to be using.
I bumped into a similar issue with a widget I'm dev'ing. I just found this:
http://codex.wordpress.org/Function_Reference/wpautop
Which I'm now going to use instead of add_filters('the_content'). I want the WYSIWYG formatting but I don't want things append to my content because it's not traditional content anymore.

Resources