Wordpress, Disqus comments and the wrong URL - wordpress

I have a Wordpress page that uses Disqus comments. At the bottom of the page I have this:
<!-- DISQUS COMMENTs -->
<div id="comments">
<div id="disqus_thread"></div>
<script>
/**
* RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
* LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/
var disqus_config = function () {
this.page.url = '<?php the_permalink(); ?>'; // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = '<?php the_ID(); ?>'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};
(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = 'https://example.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>
</div>
<!-- /#comments -->
and in my page I display how many comments have been made on the current post like so:
0 Comments
The trouble is, it's not working. The comments always appear as zero even though there are comments on the page. And I think I know why this happens, but I'm not sure how to property resolve it.
So:
My 'comments' anchor renders like so:
0 Comments
And in my JavaScript code at the bottom, the page URL gets set correctly like so:
this.page.url = 'https://www.example.com/blog/my-post-name'
However if I post a comment and log in to my Disqus control panel and hover over the post URL, the URL format is like so:
https://www.example.com/blog/?p=232
So it seems like the Disqus JavaScript is reading the URL of the page before the URL has been rewritten! Does that make sense?
A potential way to resolve it is to make my comments anchor render like so:
0 Comments
But that feels a bit hacky. Any ideas anyone?
Thanks!
UPDATE
I can confirm that rendering my comments anchor like so will work:
0 Comments
However this is more of a workaround. How can I make Disqus store my rewritten (cleaner looking) URLs instead of the Wordpress 'Plain' (and ugly) URL?

function disqus_embed($disqus_shortname) {
global $post;
wp_enqueue_script('disqus_embed','http://'.$disqus_shortname.'.disqus.com/embed.js');
echo '<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_shortname = "'.$disqus_shortname.'";
var disqus_title = "'.$post->post_title.'";
var disqus_url = "'.get_permalink($post->ID).'";
var disqus_identifier = "'.$disqus_shortname.'-'.$post->ID.'";
</script>'; }
Use this code
<?php disqus_embed('myexampleblog'); ?>//replace myexampleblog with your blog or site name register on disqus.
anywhere you want in your single.php and page.php files to embed and show Disqus comments for that page.

Related

Wordpress author page, change menu parent

I have an Author page on my Wordpress site (author.php). The problem though is that when im visiting the author page, Wordpress highlights the blog menu item. I want to have another menu item as parent. How do I go about it? I dont want to use no JS/jQuery for this.
Thanks
It's not clear that how your author page url looks like. In general its like site url/author/"author name".
If your site has a single author then you can add the author page link as a custom menu item .
Or you can use jQuery function to search "author" in your url and add active class in desired menu item
<script type="text/javascript">
$(document).ready(function () {
if(if(window.location.href.indexOf("author") > -1))
{
$("#author_menu").addClass('active');
}
});
</script>
Not the cleanest solution, but I use something like this for a few instances of current page menu highlighting for pages that aren't child/parent related in any way:
<?php if (is_author()) { ?>
<script type="text/javascript">
jQuery(function($) {
$(document).ready(function() {
$('#menu-main-menu li.menu-item-"**about_us_page_class**"').addClass('current-menu-item');
}); });
</script>
<?php } ?>

How to add a Class to the body element when a new blog post is published?

I need to add a class to my html (either the body tag or another element) after a new blog post is published, and then remove the class after a number of days.
New blog post published >>
Body gets class new-post >>
3 days later >>
new-post is removed from the body.
Any ideas how I would achieve this.
Thanks in advance
Dave
You could try this:
<?php
$blogPosts = get_posts(array('post_type' => 'post', 'status' => 'publish',));
$newestPostDate = get_the_time('U',$blogPosts[0]->ID);
$threeDaysOld = strtotime('-36 hours');
if ($newestPostDate > $threeDaysOld) { ?>
<script type="text/javascript">
jQuery(document).ready(function() {
if (jQuery('body').hasClass('new-post')) {
// do nothing
} else {
jQuery('body').addClass('new-post');
}
});
</script>
<?php } ?>
You could change 'post_type' => 'post' to any post type you want, currently it is set to get the main blog posts. This piece of code with get the newest post, and check if its date is more than 3 days old. If the date is less than 3 days old, it will check if the body already has the class new-post, if it does it will do nothing, but if it does not it will add the class. It would fire each time the page loads that you have this code on. Place it on the page template file you want it to check for anywhere after the opening HTML <body> tag. Hopefully this helps.
Write an if conditional against the_date() to check if it is less than today - 3 days

How to redirect all wordpress posts and pages to a new domain.?

i need a 301 redirect
www.old-domain.com/simple-post/ to go to www.new-domain.com (not new-domain.com/simple-post)
I tried using some .htaccess rules and did nothing,
mod-rewrite is enabled
thanks
If you are transfering your WordPress website from a domain to another, this is the way to go :
http://codex.wordpress.org/Moving_WordPress
Almost all the time theses lines do the trick :
//FIXME: do comment/remove these hack lines. (once the database is updated)
update_option('siteurl', 'http://your.domain.name/the/path' );
update_option('home', 'http://your.domain.name/the/path' );
Of course you also need to change every static links inside your posts and/or template files.
Here is a link that could help replacing links in content :
http://lorelle.wordpress.com/2005/12/01/search-and-replace-in-wordpress-mysql-database/
put this code on header.php
pass in "get_the_ID(1)" post(simple-post)id.
set redirect url.
<?php if (get_the_ID(1)) { ?>
<script type="text/javascript">
window.location = "http://www.google.com";
</script>
<?php } ?>

Make Wordpress Pages's Titles readonly

I'm looking for a WP function that add the Read-only parameter to all Pages's Titles's input, that will make the Page's title unalterable.
Thanks a lot in advance.
This can be accomplished with some simple JavaScript/jQuery. Create a file called admin_title_disable.js, and queue it up within functions.php. For example:
functions.php:
wp_register_script('admin_title_disable', '/path/to/admin_title_disable.js');
function disableAdminTitle () {
wp_enqueue_script('admin_title_disable');
}
add_action('admin_enqueue_scripts', 'disableAdminTitle');
Now, in your js file:
jQuery(document).ready(function ($) {
$('#title').attr('disabled','disabled');
});
This will set both post and page title input fields with a disabled attribute. Hope this helps!
If you want to restrict this script to a particular admin page, wrap the add_action hook in a conditional that compares $_GET['page']. You can also take advantage of the $hook parameter that is available when using admin_enqueue_scripts to check for the page. See here.
Update::
WordPress makes it a little tricky to tell between post and page edit screens, but there is a hidden input that you can take advantage of. :) Here's an updated version of the jQuery that will only run on page edit screens:
jQuery(document).ready(function ($) {
//find the hidden post type input, and grab the value
if($('#post_type').val() === 'page'){
$('#title').attr('disabled','disabled');
}
});
No need to make a seperate js file. Adding this to your function.php will do the same that Matthew showed.
function admin_footer_hook(){
?>
<script type="text/javascript">
if(jQuery('#post_type').val() === 'post'){
jQuery('#title').prop('disabled', true);
}
</script>
<?php
}
add_action( 'admin_footer-post.php', 'admin_footer_hook' );
This Solution Will disable clicking on the post title and editing it using CSS. CSS targets post type "page" only. It has been tested on Gutenberg visual editor. Users Can still edit title from "Quick Edit".
Add this code to your functions.php file.
function disable_title_edit() {
if(!current_user_can('administrator')){
if( !current_user_can('administrator')){ ////Only allow Admin
echo '<style>.post-type-page .edit-post-visual-editor__post-title-wrapper{
pointer-events: none;
}</style>'; } }
}
add_action('admin_head', 'disable_title_edit', 100);

sending arguments to block View in the tpl file

I have a block View that is displayed like so in my page.tpl.php file:
<?php if (!empty($subslider)): ?>
<div id="subslider">
<?php print $subslider; ?>
</div>
<?php endif; ?>
In the View Ui I set this default php argument:
$url = explode('/',$_GET['q']);
$slideshow = node_load($url[1]);
if($slideshow->field_slide_ref[0]['nid']){
return $slideshow->field_slide_ref[0]['nid'];
}else{
return '';
}
It grabs the reference id set in the page node.
Now, my problem is that im using page.tpl.php for other types of content that aren't necessarily nodes with a reference id in the url. I still want to pass an argument to the block View though. How do I do this in my template file?
Thanks
EDIT:
subslider is a block region. Im using Views Slideshow to make a slideshow.
I dont think I can use views_get_view_result because that just retrieves an array.. I need the actual slideshow.
In my comment, I mentioned views_embed_view(), which is different than views_get_view().
$slideshow = views_embed_view('my_slideshow_view', 'block_1', arg(1));
Also note, you don't need to explode $_GET['q']. You should be able to access URL arguments with: arg(0), arg(1), arg(2), etc....
I battled with this for a while. If you use view_embed_view() then you should read http://drupal.org/node/823056 at it explains what you have to do to make it work for a views_slideshow.
Another solution is to use JQuery. First you add the views_slideshow block to your content region. Then you create a div container on the page <div id="my-rotator"> </div> where you want it to appear (i.e. edit the node)
Then in your page.tpl.php add the following script but you'll need to replace block-views-my_image_rotator-block_2 with the name of your views block - find it by using Firebug:
<script type="text/javascript">
$(document).ready( function(){
var oNewContainer = $("div#my-rotator");
var oRotator = $("div#block-views-my_image_rotator-block_2");
oNewContainer.append(oRotator);
});
</script>
PS. You can control on what page the views-slideshow appears by configuring the block

Resources