Get the title of the original page in your default language and add it as body class to the translated page on WordPress with WPML - wordpress

Here's my problem:
add_filter( 'body_class', 'wpml_body_class');
function wpml_body_class( $class ) {
global $sitepress, $post;
if( $sitepress->get_default_language() != ICL_LANGUAGE_CODE ) {
$original_id = icl_object_id( $post->ID, get_post_type(), true, $sitepress->get_default_language() );
$class[] = strtolower(get_the_title( $original_id ));
}
return $class;
}
This code works fine. Essentially, I use $sitepress as a global to get my default language and then I extract the ID to match it with get_the_title, so, at the end of the day, I added the title as a class name to the body, so I can easily replicate the style of the original page without adding a line on my CSS stylesheet file on the translated page, in this case in French.
So far so good, except for a caveat:
Since this is the title, if I have a title like Our Team, I have to add a dash to the style, and it is going to change base on how many words I have. If I have to use the URL instead, the process to extract it with WordPress is more complex, so I was wondering if it is possible to add a regular expression to add a dash if I have any space. Or if everyone else knows how to extract the URL instead of get_the_title I couldn't be more grateful.

what you need is sanitize_title_with_dashes() for your purpose :) which is provided by WP . Reference https://codex.wordpress.org/Function_Reference/sanitize_title_with_dashes

Related

Wordpress - Make Google Fonts Array prior to final Output of Shortcode

I am trying to load custom Google Fonts with the shortcode, I have a [gfont gfont=""]Text[/gfont], so basically here is what I am doing...
function gfont_function($arr, $content = null ) {
extract(shortcode_atts(array(
'gfont' => ''
), $arr));
if(isset($arr['google_font'])) {
if($arr['google_font'] != "") {
$font_family = 'font-family:'.$arr['google_font'].', Helvetica, Verdana;';
$gfont = str_replace(' ', '+', $arr['google_font']);
wp_register_style('GoogleFonts', 'http://fonts.googleapis.com/css?family='.$gfont.'');
wp_enqueue_style('GoogleFonts');
}
}
$data = '<div style="'.$font_family.'">
'.$content.'
</div>';
return $data;
}
add_shortcode('gfont', 'gfont_function');
the above code works fine for only 1 specific font that I use, I mean the shortcode will only work as its registering the style for once..
Now my question is:
I was to check how many times this shortcode [gfont] has been used on the page before the output, so instead of using multipe urls to fetch Google Fonts, I want to make an array of all used Google Fonts and use only 1 url, something like this:
wp_register_style('GoogleFonts', 'http://fonts.googleapis.com/css?family='.ARRAY_OF_GOOGLE_FONTS.''); //formatted properly
.. so basically I will keep a global variable (most probably) where it will add all Google Fonts used on page and then make one simple formatted url to load all of them all together. Please advise.
thanks
The shortcode you've defined is odd...However, if you must do it this way, you can wrap the enqueues in wp_style_is():
// If the font hasn't already been enqueued, enqueue it
if ( ! wp_style_is( 'GoogleFonts', 'enqueued' ) ) {
wp_register_style('GoogleFonts', 'http://fonts.googleapis.com/css?family='.$gfont.'');
wp_enqueue_style('GoogleFonts');
}

Replacing only parts of an archive and single page template of WordPress

I am having a bit of trouble here understanding how to do the following. I have searched for weeks now but cannot seem to find what I am looking for.
I have a custom post type 'product' and want to change which template gets loaded for the single product page as well as the archive for the products. I am using the following code to load include and load templates.
add_filter('template_include', function() {
if (is_post_type_archive('product')) {
$templatefilename = 'archive-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
return $template;
}
if ('product' == get_post_type() ){
$templatefilename = 'single-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
return $template;
}
});
The problem I am having is that it replaces the current theme's template instead of just the inner part of the content and archive areas.
Here is what I want to achieve:
Create a custom post type 'product' in a plugin - DONE (Was kinda easy!)
When opening a single product only change the content part. - I can do this with the_content filter hook. Simple enough. Any other suggestions is welcome.
When I go to the archive view for the 'product' custom post type I don't want to have it load the theme's default archive (list) view but instead a grid view from my plugin which I cannot seem to get right. I only want to change the inner part of the template, not the whole page.
I have created this plugin a few weeks ago using only shortcodes which works good but want to see if I can do it without the use of shortcodes by means of creating the custom post type and changing the inner template parts of the current active theme.
Can anybody steer me into the right direction here?
If I create a theme I can do what I am looking for but I want to create this into a plugin instead without adding or making changes to the active theme. The plugin should handle what is needed.
The same issue is discussed here but what I want is to develop something that is theme independent. No changes should be made in theme files and no theme files should be copied to the plugin.
WP - Use file in plugin directory as custom Page Template?
Recently I also had the same problem. Here's how I worked it out.
template_include filter accepts a parameter which is the selected template that you want to override (this what you are missing in your code).
I don't know but sometimes the filter hook need higher priority to work like 9999. But first check if it work with default priority, if don't change it.
I assume your both archive and single product template both have include get_header() and get_footer() which can be used for default selected theme (Or if the theme has different setup, setup accordingly).
This is simplified code:
add_filter('template_include', function($default_template) {
if (is_post_type_archive('product')) {
$templatefilename = 'archive-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
$default_template = $template;
} else if ('product' == get_post_type() ) {
$templatefilename = 'single-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
$default_template = $template;
}
// Load new template also fallback if both condition fails load default
return $default_template;
}, 9999); // set priority, only if not worked with default one
The best option in this case is to provide a shortcode to the user. So they can place it on any page that they want (or that you auto generate). That way you will place your content inside their theme.
Something like this:
add_shortcode( 'slotsl-game', 'embed_game' );
/**
* Print the game
* #return false|string
*/
function embed_game(){
ob_start();
$game = get_post();
include_once SLOTSL_PLUGIN_DIR . 'templates/slotsl-single-game.php';
return ob_get_clean();
}

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');

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