• ahmadshc

    (@ahmadshc)


    How a post in wordpress add comment only for logged users?

    visitors must answer their own add in comments. but i need a post, leave a comment (only for logged users) and users must register to site beforce add comment to post

    can use functions file to do it ?

    i found that 2 code but not work and do not know how to use it:

    in facing link find this: https://codex.www.ads-software.com/Function_Reference/wp_get_current_user

    Code:

    <?php
    $current_user = wp_get_current_user();
    if ( 0 == $current_user->ID ) {
        // Not logged in.
    } else {
        // Logged in.
    }
    ?>

    and in facing link find this: https://codex.www.ads-software.com/Function_Reference/comment_form

    code:
    '<p class="must-log-in">' . sprintf( __( 'You must be <a href="%s">logged in</a> to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( ) ) ) ) . '</p>'

    and from the combination of these two together, I built below function!

    <?php
    $current_user = wp_get_current_user();
    if ( 0 == $current_user->ID ) {
        // Not logged in.
        '<p class="must-log-in">' .  sprintf( __( 'You must be <a href="%s">logged in</a> to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( ) ) ) ) . '</p>'
    } else {
        // Logged in.
    }
    ?>

    and add this to my post with Insert PHP Code Snippet plugin for use only (a) post

    please help me to do this. thank you all

Viewing 12 replies - 1 through 12 (of 12 total)
  • liderv

    (@liderv)

    прост зайди в консоли в “настройки” сайта,”обсуждения” там есть галочка, установи галочку где нужно и всё! Зачем усложнять себе работу

    Thread Starter ahmadshc

    (@ahmadshc)

    thank you @liderv but i need for 1 post, not all posts!
    i cant talk in russian, can type in english lang?

    liderv

    (@liderv)

    да. у меня атоматом переводится

    liderv

    (@liderv)

    авто перевод

    liderv

    (@liderv)

    если вы хотите только один пост,тогда я бы вам посоветовал его сделать на отдельную страницу и всё. а на общей странице постов под этим сделать внутреннюю ссылку на эту страницу. понятно?

    Thread Starter ahmadshc

    (@ahmadshc)

    This solution does not answer me
    In any case, I need to do this for one or some of my website posts, not all of them
    In Setting only for all

    liderv

    (@liderv)

    в описании техподдержки написано что эта функция поддерживается не всеми темами 50/50. как повезет!

    liderv

    (@liderv)

    тогда вам нужно привязать id нужного поста. найдите номер id class вашего поста и укажите что его нужно комментировать или не нужно

    Thread Starter ahmadshc

    (@ahmadshc)

    I also know that it is not supported by default.
    I want to do through wordpress hacking and functions.
    Thank you so much for helping and loving you.
    But please read my question carefully. And do not answer if you do not know the answer!
    You tell everything that I knew for myself.
    Sorry for saying that.
    Thank you very much again.

    Thread Starter ahmadshc

    (@ahmadshc)

    Nothing is impossible.
    With programming everything is possible.

    liderv

    (@liderv)

    тогда вам надо на другом форуме спрашивать , не здесь….

    Moderator bcworkz

    (@bcworkz)

    This sub-forum is perfectly fine for this topic.

    You need some way for code to distinguish which posts require special logic offered by your logged in code. All other posts would have the site settings apply. A couple of several possibilities:

    1) enumerate all applicable post IDs in an array and check if the current post ID is in the array.

    2) designate a custom field for this purpose. Check if the current post has such a field and that it has an appropriate value.

    On your theme’s post comments template, there is typically a call for comment_form() or similar function to output the form. Wrap the call with something like

    <?php
    // Post IDs that require logging in to comment
    $ids = array(123,456,789,);
    if ( in_array( get_the_ID(), $ids )) {
      // special logic applies
      if ( is_user_logged_in()) {
        comment_form();
      } else {
        // not logged in - output message and login link here
      }
    } else {
      // site defaults apply
      comment_form();
    }

    That will manage the comment form appearance on post pages. It’s possible for users to submit form data without the form because WP nonce security is lax. If it’s important to prevent this sort of hack, you need to re-check the applicability and logged in status again when the comment is submitted. For this, use the ‘pre_comment_approved’ filter and apply similar logic, except instead of calling comment_form(), return the appropriate value. Return the passed status unchanged for site defaults or user is logged in. If the post ID passed in the comment data array is in the ID array and user is not logged in, instantiate a new WP_Error object and return that.

    With programming everything is possible.

    Not quite true. You cannot do something that PHP or the server does not allow. For example, upload a 100gig file. Of course you could reconfigure the server to allow huge uploads. You can’t do something physically impossible though. Duh!

    In the case of WP specifically, there are a few things that cannot be done cleanly through themes and plugins, where core code must be hacked. While doing so is still possible, no one should be doing so.

    But as a general statement, I agree ??

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘how wordpress a post add comment only for logged users?’ is closed to new replies.