Adding a <span> to widget title in Wordpress - wordpress

I am trying to change the color of the first word in the widget title in wordpress. I am using WP v.3.7.1 and a custom child theme that I created off of the twentythirteen theme.
I just need to wrap the first word in a and can style it from there. I tried to add the following code to the function.php but it only works for one of the three widgets that I have. I have tried other widgets and it doesn't work either.
add_filter ('widget_title', 'wpzoom_fix_widgets');
function wpzoom_fix_widgets($old_title) {
$title = explode(" ", $old_title,2);
$titleNew = "<span>$title[0]</span> $title[1]";
return $titleNew;
}
Any suggestions?
Thank you!

Take a look at the first answer on WordPress Answsers, it looks close to what you are looking for:
add_filter('widget_title', my_title);
function my_title($title) {
// Cut the title to 2 parts
$title_parts = explode(' ', $title, 2);
// Throw first word inside a span
$title = '<span class="my_class">'.$title_parts[0].'</span>';
// Add the remaining words if any
if(isset($title_parts[1]))
$title .= ' '.$title_parts[1];
return $title;
}
Good luck :)

Related

Replace Parent WordPress Title Tags in Child Theme Function

Is it possible to replace the HTML of a WordPress' parent theme's title tags? I'd like to know how.
A lot of SEO plugins adjust the title tags of themes with wp_title but often developers add additional characters and words like <title><?php bloginfo('name'); ?><?php wp_title(' - ', true, 'left'); ?></title>.
I've started to develop the following inside the child theme's functions.php:
function set_title($HTMLstr, $content)
{
return preg_replace('/<title>(.*)\</title>/i', wp_title() , $HTMLstr);
}
You want a "filter", which is a function that can modify the value of arbitrary data based on a filter name.
The filter you want is called "wp_title"
http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_title
You would set it up something like:
function set_title( $title, $sep ) {
return "Custom title";
}
add_filter( 'wp_title', 'set_title', 10, 2 );
The "10" may need raised so that this filter applies after any filters in your parent theme. You can access the $title and $sep values as well, which will be passed through other filters too.
Filters typically go in functions.php.

Woocommerce override grouped product title

I've seen this on other themes but can't work out how it has been done. In my grouped product list, the title lists as Parent product title --> Child product title. I need it only to list the Child product title.
I can see the code to alter is:
' . $child_product['product']->post->post_title . '
Either the post_title needs to overridden, or the code altered to...what?
(Although this is old, it's still a common question with a prominent google ranking)
Woocommerce defines a filter, woocommerce_product_title, which allows you to pass a product's title through a function that modifies the way it will display.
Add a filter, probably in your theme's functions.php
add_filter('woocommerce_product_title', 'clean_up_title');
This is the function I'm currently using to accomplish this, no promises that it's the best way possible:
function clean_up_title($title){
// check to see if there is an html arrow in the title
if (strpos($title, '→')){
$separator = '→';
}
// otherwise assume it's the character
else {
$separator = '→';
}
// split the title into multiple parts at the arrows
$prog_array = explode($separator, $title);
// get the last part, the actual product name
$prog_name = end($prog_array);
// slice off any leading or trailing whitespace
return trim($prog_name);
}
This would be a bit cleaner to do. Just 'return' the product title rather than 'edit' it.
function clean_product_title($title, $product) {
return $product->post->post_title;;
}
add_filter('woocommerce_product_title', 'clean_product_title', 10, 2);

Single Page Navigation Menu Dynamically Generated

hHi all! I have posted this question on the WP support forums, but the community doesn't seem to be as active as stack's, so I am taking a chance here!
I am looking for a plugin that would automatically create a navigation menu (through the use of shortcodes for example) on a long single page documentation page.
The long page is divided into sections. I can imagine using a shortcode at the beginning of every section, and this will create a menu that would be displayed in a sidebar for example (called through a second shortcode perhaps, or a widget)
Any thoughts? Advice?
Thanks!
Use [section]Section Title[/section] shortcodes, then [section_navigation] where you want the navigation links output.
This works, but with a massive caveat -- that [section_navigation] needs to be in your post/page after the other [section] shortcodes... otherwise it generates an empty list.
You should be ok to use it in your theme by putting <?php echo do_shortcode("[section_navigation]");?> in sidebar.php. It will work as long as get_sidebar() is after the_content() in your theme templates (it usually is).
This to go in functions.php
$whit_sections = "";
// [section]My Section Title[/section]
function whit_section_shortcode( $atts, $title = null ) {
// $content is the title you have between your [section] and [/section]
$id = urlencode(strip_tags($title));
// strip_tags removes any formatting (like <em> etc) from the title.
// Then urlencode replaces spaces and so on.
global $whit_sections;
$whit_sections .= '<li>'.$title.'</li>';
return '<span id="'.$id.'">'.$title.'</span>';
}
add_shortcode('section', 'whit_section_shortcode');
// [section_navigation]
function whit_section_navigation_shortcode( $atts, $title = null ) {
global $whit_sections;
return '<ul class="section-navigation">'.$whit_sections.'</ul>';
}
add_shortcode('section_navigation', 'whit_section_navigation_shortcode');

Including Custom Fields & static text in the_content in WordPress

I am editing a Custom Post Type template, and am using custom fields to enter info into a meta box to be included on the page, as well as include some static default text on all the pages.
I basically need to "chunk" together the post info in the_content along with the static text and some meta box info. Here's what I want:
the_content
static text
meta box 1
more static text
meta box 2
end of the _content
I have plugins that add social buttons before the_content and a signature after the_content so I am trying to figure out how to get all my custom stuff sandwiched in between those.
If I just add the meta boxes i nthe template, they display outside of the_content and the plugins display in unwanted places.
I ended up figuring this out on my own. The solution: using functions.php and add_filter, I had to create a new function to create the default content, and it works great.
here's the general code for anyone interested:
function custom_post_type_default_content($content) {
global $post;
if ($post->post_type == 'your-custom-post-type') {
$content .= '<p> '. get_post_meta( $post->ID, "metabox-1-slug", true ).'
<br />
<p> '. get_post_meta( $post->ID, "metabox-2-slug", true ).'</p>
<p>YOUR TEXT HERE.</p>';
}
add_filter('the_content', 'custom_post_type_default_content', 0);
Note that the zero just near the end controls placement. I have a social media plugin that has a priority of "1", and to get the default content to appear above that I have to make this a priority of "0".
Also note the single apostrophes that open and close the code following $content .=
You basically add whatever you want between those apostrophes, and in this case I am pulling metabox info which have their own apostrophes containing code. It gets confusing!
In other words, your code should be $content .='YOUR CUSTOM CONTENT' and within those apostrophes, add your text, code, etc. The standalone metabox code is '. get_post_meta( $post->ID, "metabox-1-slug", true ).' which is nested inside where the YOUR CUSTOM CONTENT text is.
I am basically explaining this to myself, as these were the things that tripped me up so figured would explain them in detail to help someone else like me. Or me when I have to go look this up again!
Post your single.php here or on pastebin along with the custom field names you're using (& where you want them) and I'll try to help you figure out what you want.
Copy this code to your function.php file.
function content_function_update($content) {
global $wp_query, $post;
if ($post->post_type == 'your-custom-post-type') {
$postid = $wp_query->post->ID;
$value1 = "your value 1";
if($value1 !== '') {
$content = $content . "<br>" . $value1
}
else {
$content = $content;
}
}
return $content;
}
add_filter('the_content', 'content_function_update');
Add any custom content in variables and append it to $content variable.

Adding filters to child theme in wordpress

I have a child theme in wordpress that is based on twentyten.
Some of my authors have hardcoded URLs in their post titles and I want to remove those URLs.
I put the following code in my functions.php file in the child theme, but it has no effect on the display of the post title:
add_filter( ‘the_title’, ‘ib_strip_tags_from_titles’ );
function ib_strip_tags_from_titles( $title ) {
$title = strip_tags( $title );
return $title;
}
Any suggestions?
strip_tags() only removes HTML tags - in your case it will change the title from
Some Text LINK Other Text
to Some Text LINK Other Text
If I understand you correctly, this is what you want:
function ib_remove_links_from_titles($title) {
$title = preg_replace('/<a([^<]*)">([^<]*)<\/a>/', '', $title);
return $title;
}
add_filter( 'the_title', 'ib_remove_links_from_titles' );
going with the above example it will output Some Text Other Text
Note that given that you tried to accomplish the task with strip_tags(), I am assuming the "harcoded URLs", as you described them, are enclosed in <a [...] ></a> tags. If that's not the case you would need a regular expression that matches URLs. That is much more tricky, depending on whether the URLs your authors use are internationalized / have different domains, are not all just http:// prefaced and so on.
I vouch for the above to work if they are enclosed in tags, if not, this regex will catch most URLs, but comes without my guarantee to work in every case:
(([A-Za-z]{3,9})://)?([-;:&=\+\$,\w]+#{1})?(([-A-Za-z0-9]+\.)+[A-Za-z]{2,3})(:\d+)?((/[-\+~%/\.\w]+)?/?([&?][-\+=&;%#\.\w]+)?(#[\w]+)?)?
You'd have put that between the '/ and /' in the above function.

Resources