Made a blog on Wordpress, put the plugin, which displays the rating of the article through the stars WP-PostRatings

In the file wp-postratings.php there is such code:

 ### Function: Enqueue Ratings JavaScripts/CSS add_action('wp_enqueue_scripts', 'ratings_scripts'); function ratings_scripts() { $postratings_max = intval(get_option('postratings_max')); $postratings_custom = intval(get_option('postratings_customrating')); $postratings_ajax_style = get_option('postratings_ajax_style'); $postratings_javascript = ''; if($postratings_custom) { for($i = 1; $i <= $postratings_max; $i++) { $postratings_javascript .= 'var ratings_'.$i.'_mouseover_image=new Image();ratings_'.$i.'_mouseover_image.src=ratingsL10n.plugin_url+"/images/"+ratingsL10n.image+"/rating_'.$i.'_over."+ratingsL10n.image_ext;'; } } else { $postratings_javascript = 'var ratings_mouseover_image=new Image();ratings_mouseover_image.src=ratingsL10n.plugin_url+"/images/"+ratingsL10n.image+"/rating_over."+ratingsL10n.image_ext;'; } wp_enqueue_script('wp-postratings', plugins_url('wp-postratings/postratings-js.js'), array('jquery'), null, true); wp_localize_script('wp-postratings', 'ratingsL10n', array( 'plugin_url' => plugins_url('wp-postratings'), 'ajax_url' => admin_url('admin-ajax.php'), 'text_wait' => __('Please rate only 1 post at a time.', 'wp-postratings'), 'image' => get_option('postratings_image'), 'image_ext' => RATINGS_IMG_EXT, 'max' => $postratings_max, 'show_loading' => intval($postratings_ajax_style['loading']), 'show_fading' => intval($postratings_ajax_style['fading']), 'custom' => $postratings_custom, 'l10n_print_after' => $postratings_javascript )); } 

This code introduces the following code into the body of ALL pages (on the main page, in categories, on the 404 page and indeed everywhere else):

 <script type='text/javascript'> /* <![CDATA[ */ var ratingsL10n = {"plugin_url":"https:\/\/site.com\/wp-content\/plugins\/wp-postratings","ajax_url":"https:\/\/site.com\/wp-admin\/admin-ajax.php","text_wait":"\u041f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430, \u043d\u0435 \u0433\u043e\u043b\u043e\u0441\u0443\u0439\u0442\u0435 \u0437\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e.","image":"stars_flat(png)","image_ext":"png","max":"5","show_loading":"0","show_fading":"0","custom":"0"}; var ratings_mouseover_image=new Image();ratings_mouseover_image.src=ratingsL10n.plugin_url+"/images/"+ratingsL10n.image+"/rating_over."+ratingsL10n.image_ext;; /* ]]> */ </script> <script type='text/javascript' src='https://site.com/wp-content/plugins/wp-postratings/postratings-js.js'></script> :" \ u041f \ u043e \ u0436 \ u0430 \ u043b \ u0443 \ u0439 \ u0441 \ u0442 \ u0430, \ u043d \ u0435 \ u0433 \ u043e \ u043b \ u043e \ <script type='text/javascript'> /* <![CDATA[ */ var ratingsL10n = {"plugin_url":"https:\/\/site.com\/wp-content\/plugins\/wp-postratings","ajax_url":"https:\/\/site.com\/wp-admin\/admin-ajax.php","text_wait":"\u041f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430, \u043d\u0435 \u0433\u043e\u043b\u043e\u0441\u0443\u0439\u0442\u0435 \u0437\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e.","image":"stars_flat(png)","image_ext":"png","max":"5","show_loading":"0","show_fading":"0","custom":"0"}; var ratings_mouseover_image=new Image();ratings_mouseover_image.src=ratingsL10n.plugin_url+"/images/"+ratingsL10n.image+"/rating_over."+ratingsL10n.image_ext;; /* ]]> */ </script> <script type='text/javascript' src='https://site.com/wp-content/plugins/wp-postratings/postratings-js.js'></script> 

But it is absolutely not needed on ALL pages, but only on pages of records. Because the asterisks are connected ONLY there.

I think that it is necessary to wrap some part of the code with is_single , but without being a programmer I won’t cope :-(

Tell me, please, how to implement it.

PS

I want to pay special attention to the fact that the code added to all pages is auxiliary (required for the correct operation of the stars themselves). The code for the stars themselves is outputted by the command <?php if(function_exists('the_ratings')) { the_ratings(); } ?> <?php if(function_exists('the_ratings')) { the_ratings(); } ?> and it looks like this in the body of the page in this way:

 <div id="post-ratings-121" class="post-ratings" itemscope="" itemtype="http://schema.org/Article" data-nonce="d927a6021d"><img id="rating_121_1" src="https://site.com/wp-content/plugins/wp-postratings/images/stars_flat(png)/rating_off.png" alt="Ужасная статья" title="Ужасная статья" onmouseover="current_rating(121, 1, 'Ужасная статья');" onmouseout="ratings_off(0, 0, 0);" onclick="rate_post();" onkeypress="rate_post();" style="cursor: pointer; border: 0px;"><img id="rating_121_2" src="https://site.com/wp-content/plugins/wp-postratings/images/stars_flat(png)/rating_off.png" alt="Так себе статья" title="Так себе статья" onmouseover="current_rating(121, 2, 'Так себе статья');" onmouseout="ratings_off(0, 0, 0);" onclick="rate_post();" onkeypress="rate_post();" style="cursor: pointer; border: 0px;"><img id="rating_121_3" src="https://site.com/wp-content/plugins/wp-postratings/images/stars_flat(png)/rating_off.png" alt="Нормальная статья" title="Нормальная статья" onmouseover="current_rating(121, 3, 'Нормальная статья');" onmouseout="ratings_off(0, 0, 0);" onclick="rate_post();" onkeypress="rate_post();" style="cursor: pointer; border: 0px;"><img id="rating_121_4" src="https://site.com/wp-content/plugins/wp-postratings/images/stars_flat(png)/rating_off.png" alt="Хорошая статья" title="Хорошая статья" onmouseover="current_rating(121, 4, 'Хорошая статья');" onmouseout="ratings_off(0, 0, 0);" onclick="rate_post();" onkeypress="rate_post();" style="cursor: pointer; border: 0px;"><img id="rating_121_5" src="https://site.com/wp-content/plugins/wp-postratings/images/stars_flat(png)/rating_off.png" alt="Отличная статья!" title="Отличная статья!" onmouseover="current_rating(121, 5, 'Отличная статья!');" onmouseout="ratings_off(0, 0, 0);" onclick="rate_post();" onkeypress="rate_post();" style="cursor: pointer; border: 0px;"> оцените материал<br><span class="post-ratings-text" id="ratings_121_text" style="display: none;"></span><meta itemprop="headline" content="ЗАГОЛОВОК СТАТЬИ"><meta itemprop="description" content="КОНТЕНТ СТАТЬИ (ОТРЫВОК)"><meta itemprop="datePublished" content="ДАТА СТАТЬИ"><meta itemprop="url" content="URL СТАТЬИ"></div> 

The code of the stars themselves is displayed only on the pages of the records, and the auxiliary code for some reason EVERYWHERE. I want to fix this.

  • Do not forget that after updating the plug-in, these settings will disappear because you are editing the plugin files directly - eugene_v
  • Such things I do not forget. I always make backup and besides it I also keep a log of changes, which I did myself in the code of files and plugins :-) - UserUser-name

1 answer 1

Try adding a condition with a check for the type of is_singular() records https://codex.wordpress.org/Function_Reference/is_singular

 add_action('wp_enqueue_scripts', 'ratings_scripts'); function ratings_scripts() { if( ! is_singular('post') ) return false; $postratings_max = intval(get_option('postratings_max')); $postratings_custom = intval(get_option('postratings_customrating')); $postratings_ajax_style = get_option('postratings_ajax_style'); $postratings_javascript = ''; if($postratings_custom) { for($i = 1; $i <= $postratings_max; $i++) { $postratings_javascript .= 'var ratings_'.$i.'_mouseover_image=new Image();ratings_'.$i.'_mouseover_image.src=ratingsL10n.plugin_url+"/images/"+ratingsL10n.image+"/rating_'.$i.'_over."+ratingsL10n.image_ext;'; } } else { $postratings_javascript = 'var ratings_mouseover_image=new Image();ratings_mouseover_image.src=ratingsL10n.plugin_url+"/images/"+ratingsL10n.image+"/rating_over."+ratingsL10n.image_ext;'; } wp_enqueue_script('wp-postratings', plugins_url('wp-postratings/postratings-js.js'), array('jquery'), null, true); wp_localize_script('wp-postratings', 'ratingsL10n', array( 'plugin_url' => plugins_url('wp-postratings'), 'ajax_url' => admin_url('admin-ajax.php'), 'text_wait' => __('Please rate only 1 post at a time.', 'wp-postratings'), 'image' => get_option('postratings_image'), 'image_ext' => RATINGS_IMG_EXT, 'max' => $postratings_max, 'show_loading' => intval($postratings_ajax_style['loading']), 'show_fading' => intval($postratings_ajax_style['fading']), 'custom' => $postratings_custom, 'l10n_print_after' => $postratings_javascript )); } 
  • Many thanks, Vitaly! This short line realizes the conceived in full :-) - UserUser-name
  • It's my pleasure. Now the main thing is not to update the plugin)) - Vitaliy Kukin