Notifications
Clear all

Specify post ID when commenting

9 Posts
4 Users
1 Reactions
2,182 Views
(@mathieufd)
Active Member
Joined: 6 years ago
Posts: 10
Topic starter  

Hello,

For a specific and important reason, I needed to add the full comment template of a specific post somewhere else (in another page, or in homepage). I succeed to do that. Now the problem is that when I want to publish a comment, it doesn't publish the comment on the good post. I understand why because when publishing a comment it usually "take" the "natural" post ID when it's on single.php

Is there a way somewhere in your plugin (code) where I could specify a post ID when posting a comment ? At least could you specify which of your file I should look at?

Thank you


   
Quote
Astghik
(@astgh)
Illustrious Member Admin
Joined: 7 years ago
Posts: 6147
 

Hi @mathieufd,

you should use the wpdiscuz_js_options hook, and change the value of the wc_post_id key (set it the current post ID ). 

 

 


   
ReplyQuote
(@mathieufd)
Active Member
Joined: 6 years ago
Posts: 10
Topic starter  

Great! 

Where should I look at? (didn't find the hook)


   
ReplyQuote
Astghik
(@astgh)
Illustrious Member Admin
Joined: 7 years ago
Posts: 6147
 

@mathieufd,

you can find it in the wp-content/plugins/wpdiscuzclass.WpdiscuzCore.php file. 


   
mathieufd reacted
ReplyQuote
(@mathieufd)
Active Member
Joined: 6 years ago
Posts: 10
Topic starter  

Alright, thank you.

Is there a way to clean the post_ID that have been taking in account so far (without refresh of course) ? I need that because I don't refresh the page when I change the post to be commented. Everything is loaded with ajax. Each time I load your template, I redefined wpdiscuzAjaxObj with the right Post_ID by loading one more time (screenshot) the var wpdiscuzAjaxObj (ajax).

Capture d’écran 2019 03 11 à 15.49.16

So when I change wc_post_id dynamically, the correct post ID is displayed in wpdiscuzAjaxObj (var console gives me the good post ID). So if I open (ajax) the post form with a post ID #123, I will be able to comment for the post #123. But the problem is when I open a second form (ajax) without refreshing, I will be able to comment the seconde post but it will ALSO comment the first one and even if I redefine wpdiscuzAjaxObj, both posts will be commented.

I tried to redefine :

  • var wpdiscuzAjaxObj
  • var wpdiscuzPostId
  • var postId

 

 
This post was modified 6 years ago by mathieufd

   
ReplyQuote
Astghik
(@astgh)
Illustrious Member Admin
Joined: 7 years ago
Posts: 6147
 

Hi @mathieufd,

I apologize for the late response. 

Is there a way to clean the post_ID that have been taking in account so far (without refresh of course) ?

Yes, we've checked one more time and found that this cannot be reached yet. We'll take into consideration to change this in future version release. 


   
ReplyQuote
(@diyafury)
New Member
Joined: 5 years ago
Posts: 2
 

Hi,

It's probably a bit late but I found a solution for this... basically I wanted to display wpdiscuz comments for photo posts in popups. My functionality exists in a plugin itself. Here's how I did it.

1. wpdiscuz uses the global $post->ID by default for comment functionality. This means the page id rather than photo id is being used when a comment within a photo popup is posted. So... firstly I needed to find a way to pass the photo id to the wpdiscuz plugin.

2. To do this I looked in wpdiscuz plugins directory > class.WpdiscuzCore.php. A wpdiscuz-ajax-js script is enqueued here and then the variable wpdiscuzAjaxObj is localised. This variable contains an object with the post_id as a property to be used for ajax operations. I specifically needed to update this variable on a click event of each call-to-action child within a comment wrapper element (I wrapped a <div> around each photo's comments area). On the comment wrapper I specified a data-pid attribute containing the photo id. Now the event handler on the child element can reference this when clicked, eg.

$(document).on('click', '.wpdiscuz-sort-button, input.wpd_label__checkbox, .wc_comm_submit, .wc-vote-link, .wc-cta-button', function(e) {

wpdiscuzAjaxObj.wpdiscuz_options.wc_post_id = $(this).closest('.comment-wrap').data('pid');

});

3. The enqueued wpdiscux-ajaz-js script (wpdiscuz.js) sets a local variable in an encapsulated jquery function to point to the value of the localised wpdiscuzAjaxObj variable (that I set on click), i.e.

 

var wpdiscuzPostId = wpdiscuzAjaxObj.wpdiscuz_options.wc_post_id;

This variable is out of reach from my script so I took a copy of this file in my plugin and customised it by commenting out the above assignment and replacing any usages of wpdiscuzPostId with the global variable wpdiscuzAjaxObj.wpdiscuz_options.wc_post_id.

 
4. To use this file I dequeued the original, wpdiscux-ajaz-js in my plugin and enqueue my amended version, custom-wpdiscuz-ajax-js.
 
5. With this newly queued file I needed to localize the existing variable, wpdiscuzAjaxObj against it... to do this, I grabbed the value of the original variable, which is returned as a string, converted it to an array and localised it against my custom custom-wpdiscuz-ajax-js script, eg.
 
global $wp_scripts;

$wpdiscuzOptionsJs = $wp_scripts->get_data('wpdiscuz-ajax-js', 'data');

if ( !is_array($wpdiscuzOptionsJs) ) {

$wpdiscuzOptionsJs = json_decode(str_replace('var wpdiscuzAjaxObj = ', '', substr($wpdiscuzOptionsJs, 0, -1)), true);

}

if ( is_page('xxx') ) {

// Slight modified copy of wpdiscuz needed for this page to accept passed in/popup post ids, rather than using the default post id.

wp_dequeue_script( 'wpdiscuz-ajax-js' );

wp_enqueue_script( 'custom-wpdiscuz-ajax-js', PHOTO_PLUGIN_URL . 'stream/js/wpdiscuz.js', array('jquery'), 'custom.5.3.5', true );

wp_localize_script('custom-wpdiscuz-ajax-js', 'wpdiscuzAjaxObj', array('url' => admin_url('admin-ajax.php'), 'customAjaxUrl' => plugins_url() . '/wpdiscuz/utils/ajax/wpdiscuz-ajax.php', 'wpdiscuz_options' => $wpdiscuzOptionsJs));
}
That's it! So now with every comment call-to-action the global variable is updated with the photo id and used directly by the custom wpdiscuz.js file. I hope this makes sense.
 

   
ReplyQuote
(@diyafury)
New Member
Joined: 5 years ago
Posts: 2
 

I tried to edit my previous post but something went wrong, small changes to the enqueue script (better naming and a slight simplification):

global $wp_scripts;

$wpdiscuzAjaxData = $wp_scripts->get_data('wpdiscuz-ajax-js', 'data');

if ( !is_array($wpdiscuzAjaxData) ) {

$wpdiscuzAjaxData = json_decode(str_replace('var wpdiscuzAjaxObj = ', '', substr($wpdiscuzAjaxData, 0, -1)), true);

}

if ( is_page('xxx') ) {

// Slight modified copy of wpdiscuz needed for this page to accept passed in/popup post ids, rather than using the default post id.

wp_dequeue_script( 'wpdiscuz-ajax-js' );

wp_enqueue_script( 'custom-wpdiscuz-ajax-js', MOP_PHOTO_PLUGIN_URL . 'stream/js/wpdiscuz.js', array('jquery'), 'custom.5.3.5', true );

wp_localize_script( 'custom-wpdiscuz-ajax-js', 'wpdiscuzAjaxObj', $wpdiscuzAjaxData );
}

   
ReplyQuote
Elvina
(@elvina)
Support
Joined: 5 years ago
Posts: 1403
 

@diyafury,

Thank you for sharing the information.

 


   
ReplyQuote
Share:
Scroll to top