In a WordPress woocommerce template, this line outputs woocomerce product description / excerpt
<?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ) ?>
Adding my HTML after that line outputs my HTML outside the div that holds the excerpt/description of the product. I am trying to add a div inside the content as if it was added in the editor.
Basically, I want to add something in all products but because I have many products already, I want a way to insert it in template file or something just once and applied to all.
Anyone know how can I do that?
You could add a filter that adds the div, before the woocommerce software adds it. Try something like this, inside your <theme>/functions.php file:
function add_my_div($desc) {
return '<div class="my-div">'.$desc.'</div>';
}
add_filter('woocommerce_short_description', 'add_my_div', 0, 1);
Notice it is on priority 0. This should run before most other filters. The default value for priority is 10, and I am pretty sure that is the priority that WC adds it's wrapper div.
Hope this helps.
jquery .append did the trick for me
http://api.jquery.com/append/
#loushou Thanks for your comment. It looks like your suggestion could work also but I haven/t tried it yet. Will do If I got the time and update my findings here.
Thanks
Related
First, this is what I'm trying to do - add code directly before </head> and </body> in my Wordpress site. The code must be at that exact location. I don't want to use another plugin to accomplish it though.
I'm using a child theme as I understand the structure of child themes and how they're supposed to work.
This is what I've tried:
Used add_action( 'wp_head', function ) and add_action( 'wp_footer', function ). The code does get added but doesn't appear where I expect i.e. there is other code inserted after mine and before the closing tags.
Taken footer.php and header.php from the parent theme, added them to my child theme and then added the code snippets. The code in footer.php does not get inserted at all. The code in header.php does get inserted but there is other code inserted between mine and the </body> tag.
I need my snippets directly before the </head> and </body> tags, but can't seem to make them appear exactly where I want. What am I doing wrong?
Thanks!
Most likely another script is running after yours doing the same thing.
If you can't figure out what plugin or script this causes by looking at the inserted code, I'd do a project wide search on "wp_head" and "wp_footer" to find out where and when this gets fired exactly.
Not much else we can do here.
Did you try "priority" attr in add_action( 'wp_head()', function(), 99999 ) ?
Increase the priority number, until your function is executed last.
I use a product category description in every products category in woocommerce, but i don't know how to show the full description. After a few words it just stops and shows "..."
https://i.stack.imgur.com/BaOx5.jpg
There are 2 general ways I'm aware the text would show ... at the end. Either via code or by CSS text-overflow: ellipsis; If it's using the CSS method, then you got to inspect the element (try right-click on element and choose Inspect, or something similar to that), see which html tag has the style set and then remove it or override it in the stylesheet.
(BTW: If you notice that the full description is there when inspecting the element, then text-overflow CSS is most likely involved.)
Read more here: https://developer.mozilla.org/en-US/docs/Web/CSS/text-overflow
If it's not CSS but done by code, then it's necessary to get more details on the code used for this to know how to change it. It can be done either by JavaScript or PHP and might be dependent on the WordPress Theme in use.
A quick solution is to observer the source code for the page. See if the full text is in use anywhere else on the page, and then use JavaScript to rewrite the text in the Description area based on it. Read more http://api.jquery.com/html/
Otherwise, you will have to find out if there are any API Hooks that can intercept the Description area to change the text. (Maybe https://codex.wordpress.org/Function_Reference/category_description or http://hookr.io/actions/woocommerce_archive_description/)
Again it might be dependent on the theme. If you are not getting through, add the Theme name to your question, to try get more help.
Here's an example of how to use woocommerce_archive_description
add_action( 'woocommerce_archive_description', 'woo_category_description' );
function woo_category_description() {
if ( is_product_category() ) {
global $wp_query;
$cat_id = $wp_query->get_queried_object_id();
$cat_desc = term_description( $cat_id, 'product_cat' );
$description = '<div class="description">'.$cat_desc.'</div>';
echo $description;
}
}
After digging internet for an answer I cannot find any idea about how to deal with my issue. I think the problem is common for someone who knows PHP a little bit.
To describe the situation. For some custom WordPress plugin I've got two PHP files: ff_config.php and loantest_form.php. First file contains some configurations of plugin plus following lines:
/**--------------------------TABLE SHORTCODES-----------------------*/
function render_loantest_form() {
include(plugin_dir_path(__FILE__) . 'front/loantest_form.php');
}
add_shortcode( 'render_loantest_form' , render_loantest_form );
/**--------------------------DISPLAY PLUGIN IN FOOTER-----------------------*/
add_action('wp_footer', 'display_loantest');
function display_loantest() {
echo render_loantest_form();
}
Which I suppose rendering second file containing enqueue scripts (js/css) and whole HTML output and placing in wp_footer where it exactly is on my page.
The question is: how to change mentioned lines to allow me to place render result (loantest_form.php) in specific div / id on page (for example #sidebar-slider)?
If you want to display your shortcode in a template file,
echo do_shortcode('[render_loantest_form]');
Enable the use of shortcodes in text widgets.
add_filter( 'widget_text', 'do_shortcode' );
And inside the text editor
[render_loantest_form]
You can read more about do_shortcode()
Note that you need to be sure that the file load in the render function is good and that it returns the expect content.
How can I place a default text (hashtag) in the Custom Message?
The textarea is (located in line 643) under jetpack/modules/publicize/ui.php
I tried to put the text in front of $title in various ways, like:
<?php echo "#myhashtag $title"; ?>
or
<?php echo '#myhashtag '.$title; ?>
but it just echoes the text, not the $title.
Any ideas will be greatly appreciated.
You can use the approach of this Wordpress plugin i made (Publicize With Hashtags), which does exactly that. It basically use and action trigger bound to the 'save_post' native event.
If you want to develop your own one you can have a look at my Source Code on the project's GitHub page or in this installation & usage guide I wrote about it.
You can add a filter, like so, to your theme's functions.php or a site-specific plugin:
add_filter( 'wpas_default_prefix', 'add_default_publicize_hashtag_prefix', 10, 4 );
function add_default_publicize_hashtag_prefix() {
$default_tags = '#yourhastaghere ';
return $default_tags;
}
This will add your default hashtag before your title without you needing to hack the WordPress core.
jetpack/modules/publicize/ui.php itself states in its comments:
/**
* Only user facing pieces of Publicize are found here.
*/
You added your hashtag in the textarea which allows admins to enter a custom message (click edit and it will slide down with your hashtag).
As #Yazmin mentioned, the best way to permanently edit the message is using a filter. The filters available are wpas_default_prefix, wpas_default_message, and wpas_default_suffix.
Personally, I had no success using these filters and I'm interested in a working solution to this issue myself.
I am not really sure how to ask this question, however if you have a look at this link you will see what I mean
http://www.skirentalwhistler.com/skill-level/beginner
You will see that the shortcodes for a [box] are not being executed.
If you browse a single product, the shortcode works fine - eg click on the first product called 'Compact' and you will see the [box] is executed into the grey box with one grey and one orange button in it.
Also, I have spent a few hours now trying to work out which template is creating this page. The page is a listing of all products with a certain product attribute value (attribute is skill-level and value is beginner)
I expected it to be archive-product.php but it doesn't seem to be, and I can't find any other file dealing with multiple products on a single page
Any help is appreciated!
When you are using a shortcode in a post, then the [box] syntax is what to use. However, if you are doing a shortcode in a template, you must execute the shortcode with php. Since your homepage appears to just display the short code instead of running it, then it is probably in a template. To execute a shortcode in a template, do this
<?php echo do_shortcode('[box]'); ?>
so for the one on your home page
<?php echo do_shortcode('[box size="small" style="rounded" border="full"]'); ?>
Ok, I have worked out how to do it.
Big thanks to chiliNUT for their help, as they helped me to keep going and going.... thus I gave them an uptick
Anyway, it was using the themes archive.php template (not the woocommerce plugin templates as I presumed). In archive.php, I changed the following line in the div class 'entry'
<?php if ( $woo_options[ 'woo_post_content' ] == "content" ) the_content(__( 'Read More...', 'woothemes' )); else the_excerpt(); ?>
to
<?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ) ?>
It now works and doesn't seem to have broken anything else.