How to change Tag tooltip as Tag Description?
I tried and my code is mentioned below.
function title_text( $description ) {
return sprintf( _n('%s topic', '%s', $description), $description );
}
wp_tag_cloud( array( 'topic_count_text_callback' => 'title_text') );
This is not working. Can any one please check this code and find me a correct solution?
Related
The following works for the Product page but it does not work for the Shop page.
<?php
add_filter('woocommerce_sale_flash', 'woocommerce_custom_sale_text', 10, 3);
function woocommerce_custom_sale_text($text, $post, $_product)
{
return '<span class="onsale">PUT YOUR TEXT</span>';
}
Please suggest modifications.
Thanks!
Use This
add_filter( 'woocommerce_sale_flash', 'wooc_custom_replace_sale_text' );
function wooc_custom_replace_sale_text( $html ) {
return str_replace( __( 'Sale!', 'woocommerce' ), __( 'Your Text', 'woocommerce' ), $html );
}
I tried your code and it works perfectly for the shop page as well. You may try increasing the priority or it can be a conflict with some other plugin or theme.
You may also check the following file to confirm it has applied the woocommerce_sale_flash filter
woocommerce\templates\loop\sale-flash.php
I'm working on a project that requires comments to have tags and be searchable by tags. Is there a way to implement it in WP, or should I look for some workaround (like create child post type instead of comments and apply tags to it)?
If there is, how can I do it?
Thank you.
You can use comment meta to store and retrieve tags of a particular comment.
First of all, add the tag field to the comment form and populate the tags. The following code will add a "select" field immediately after comment textarea and populate it with the tags.
add_filter( 'comment_form_defaults', 'change_comment_form_defaults');
function change_comment_form_defaults( $default ) {
$commenter = wp_get_current_commenter();
$out = '<label for="comment_tags">Tags:</label><select name="comment_tags" multiple>';
foreach ( get_tags() as $tag ) {
$out .= '<option value="<?php echo $tag->term_id; ?>"><?php echo $tag->name; ?></option>';
}
$out .= '</select>';
$default[ 'comment_field' ] .= $out;
return $default;
}
The comment_post action is triggered immediately after a comment is stored in the database. You can use it to store post meta.
add_action('comment_post', 'add_tags_to_comment', 10, 2);
function add_tags_to_comment( $comment_ID, $comment_approved ) {
foreach($_POST["comment_tags"] as $comment_tag) {
add_comment_meta( $comment_ID, "comment_tag", $comment_tag );
}
}
Instead of storing the selected tags as an array in a single record, I prefer to store each tag as a separate record. This will make it easier to search the comments based on tags.
When you want to retrieve the tags of a comment, You can get_comment_meta
$tags = get_comment_meta($comment_ID, "comment_tag");
foreach($tags as $tag_id) {
$tag_term = get_term($tag_id, 'post_tag');
echo $tag_term->name;
}
Use WP_Comment_Query to search comments based on tags.
$tags = array(1,32,5,4); /* Replace it with tags you want to search */
$args = array(
'meta_query' => array(
array(
'key' => 'comment_tag',
'value' => $tags,
'compare' => 'IN'
)
)
);
$comment_query = new WP_Comment_Query( $args );
Hope this helped you.
I'm creating a plugin with an admin settings page, and I want to use the settings API.
I am able to save text fields and checkboxes, but when I add a settings field with a wp_editor it's not saving..?
I used to do this with get_option, but now I want to use a settings class and the register_setting() method.
This is my code:
public function register_settings() {
register_setting( 'eman_setting', 'eman_setting', array( $this, 'eman_validate_settings' ) );
add_settings_field(
'eman_dashboard_welcome',
__( "", 'emanlang' ),
array( $this, 'eman_dashboard_welcome_callback' ),
'management-settings',
'eman_settings_section'
);
}
public function eman_dashboard_welcome_callback() {
//$content = 'Empty';
$editor_id = 'textareadashboardwelcome';
$args = array('textarea_name' => 'eman_dashboard_welcome');
if(! empty( $this->eman_setting['eman_dashboard_welcome'] ) ) {
$content = $this->eman_setting['eman_dashboard_welcome'];
} else {
$content = 'The field is empty';
}
wp_editor( $content, $editor_id, $args );
/** TESTING **/
echo '<br /><b>testing textarea output: </b>'. $content .'<br /><br />';
echo '<b>Complete settings array dump: </b><br />';
echo var_dump($this->eman_setting);
}
Note: This is only the relevant part of the code. On this page I have multiple 'add_settings_field' which are all working fine.
As you may have noticed, for testing I do a var_dump() to check what's inside the options array.
The dump returns:
array(3) { ["eman_opt_in"]=> string(2) "on" ["eman_sample_text"]=> string(10) "sample 1.1" ["eman_sample_text2"]=> string(10) "sample 2.2" }
After saving the form, the array only contains 3 fields, so the array is not even populated with the [eman_dashboard_welcome]?
I tried many possible solutions, like adding this jQuery:
$('#submit').mousedown( function() {
tinyMCE.triggerSave();
});
But nothing works... Please help :-)
Found it..
Solution:
Textarea / wp_editor 'name' needs to call the field in the array.
In my case:
$args = array('textarea_name' => 'eman_setting[eman_dashboard_welcome]');
wp_editor( $content, $editor_id, $args );
I'm trying to add some action links to a WordPress plugin. I started with the following.
class Angelleye_PayPal_WooCommerce
{
public function __construct()
{
add_filter('plugin_action_links', array($this,'plugin_action_links'));
}
public function plugin_action_links($actions)
{
$custom_actions = array(
'configure' => sprintf( '%s', admin_url( 'admin.php?page=wc-settings&tab=checkout' ), __( 'Configure', 'paypal-for-woocommerce' ) ),
'docs' => sprintf( '%s', 'http://docs.angelleye.com/paypal-for-woocommerce/', __( 'Docs', 'paypal-for-woocommerce' ) ),
'support' => sprintf( '%s', 'http://www.angelleye.com/contact-us/', __( 'Support', 'paypal-for-woocommerce' ) ),
'review' => sprintf( '%s', 'http://wordpress.org/support/view/plugin-reviews/paypal-for-woocommerce', __( 'Write a Review', 'paypal-for-woocommerce' ) ),
);
// add the links to the front of the actions list
return array_merge( $custom_actions, $actions );
}
}
This works except that it puts the links on every single plugin that's currently enabled instead of just my own. I'm looking at the WordPress codex info about this, and it shows to use the filename appended to the filter name. So I made the adjustment like this:
add_filter('plugin_action_links_'.__FILE__, array($this,'plugin_action_links'));
When I do that, though, all of the links go away altogether and they don't show up anywhere, not even my own. What am I doing wrong here?
As explained by Akshay, we need to use the plugin_basename as suffix for the hook. But for completeness, a couple of missing details.
The hook can also take a prefix to show the action links in the Network screen of a Multisite installation:
$basename = plugin_basename( __FILE__ );
$prefix = is_network_admin() ? 'network_admin_' : '';
add_filter(
"{$prefix}plugin_action_links_$basename",
array( $this,'plugin_action_links' ),
10, // priority
4 // parameters
);
The hook takes 4 parameters, which may contain useful information for building the links:
public function plugin_action_links( $actions, $plugin_file, $plugin_data, $context )
{
// $plugin_file is the plugin_basename
// $plugin_data contains the plugin's header information
// $context is the current screen (all: All plugins, active: Active plugins)
}
If we use the hook without the basename suffix, we can use the $plugin_file param to filter out only our plugin(s).
Use plugin_basename( __FILE__ ) instead of __FILE__.
Use following filter to add action links.
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array($this, 'plugin_action_links') );
I had working this filter in one of my plugin, hope its work for you too.
use &$this instead of $this,
add_filter('plugin_action_links_' . $this->plugin_file, array(&$this, 'settings_links'), 10, 1);
i want to force the attribute "alt" on thumbnail when admin add new thumbnail on wordpress.
if there is javascript hook on saving thumbnail validator on the input "title" that's well be great!
Thank you.
Found this, place it in your functions.php file in your theme directory
function add_alt_tags($content)
{
global $post;
preg_match_all('/<img (.*?)\/>/', $content, $images);
if(!is_null($images))
{
foreach($images[1] as $index => $value)
{
if(!preg_match('/alt=/', $value))
{
$new_img = str_replace('<img', '<img alt="'.$post->post_title.'"', $images[0][$index]);
$content = str_replace($images[0][$index], $new_img, $content);
}
}
}
return $content;
}
add_filter('the_content', 'add_alt_tags', 99999);
Find more here http://www.paulund.co.uk/add-missing-alt-tags-to-wordpress-images
This is going on my snippet site. Hope it helps
By default, the image returned does not have a title or alt attribute. (Since WordPress 4.7, the alt attribute is no longer added automatically. It will only have an alt attribute if you specifically entered "Alt text" while uploading the image, or if you go back to the Media library and enter "Alt text" for the image).
Currently website(s) traffic from Google search is considerably higher if your images all have (title attribute tags), as well as (alt tags). So, I add the title and alt attributes to post thumbnails with the following function which goes in your theme "functions.php" file. The value for the title and alt attributes will be taken from the title of the image, which is the title of the attachment (not the actual post title).
function eln_add_img_title( $attr, $attachment = null ) {
$img_title = trim( strip_tags( $attachment->post_title ) );
$attr['title'] = $img_title;
$attr['alt'] = $img_title;
return $attr;
}
add_filter( 'wp_get_attachment_image_attributes','eln_add_img_title', 10, 2
);
If you don't want the image attributes taken from default image name
you can change the code so the image attributes taken from
"post_title" as follow:
// Force adding missing image alt & title for WordPress.
function eln_add_img_title( $attr, $attachment = null ) {
$img_title = trim( strip_tags( $attachment->post_title ) );
$attr['title'] = the_title_attribute( 'echo=0' );
$attr['alt'] = the_title_attribute( 'echo=0' );
return $attr;
}
add_filter( 'wp_get_attachment_image_attributes','isa_add_img_title', 10, 2 );
Hope this help you and save your time, Have a nice day :)