• Resolved Iurie Malai

    (@flegmatiq)


    Greg, I try to hide the form when registration is disabled or users are not logged in. You recommended here to use a membership plugin, but another plugin for this probably is too much. I found this in the shortcode.php file for the [adverts_manage] shortcode:

    if(!get_current_user_id()) {
        ...
        $permalink = get_permalink();
        $message = __('Only logged in users can access this page. <a href="%1$s">Login</a> or <a href="%2$s">Register</a>.', "adverts");
        $parsed = sprintf($message, wp_login_url( $permalink ), wp_registration_url( $permalink ) );
        adverts_flash( array( "error" => array( $parsed ) ) );
        ...
    }

    Can be added something like this also for the [adverts_add] shortcode?

    I tried to adapt this snippet to hide/unset the form when registration is disabled or user is not logged in, but without success. Can the form be hided in this way?

    if ( !get_option( 'users_can_register' ) || !get_current_user_id() ) {
        if($field['name'] == '_adverts_account') {
            unset($form['field'][$k]);
    
            $permalink = get_permalink();
            $message = __('Attention! Only logged in users can add adverts. <a href="%1$s">Login</a> or <a href="%2$s">Register</a>.', "adverts");
            $parsed = sprintf($message, wp_login_url( $permalink ), wp_registration_url( $permalink ) );
            adverts_flash( array( "error" => array( $parsed ) ) );
        }
        // A CODE THAT WILL HIDE/UNSET THE FORM HERE ???
    }

    https://www.ads-software.com/plugins/wpadverts/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Greg Winiarski

    (@gwin)

    You should rather create your own shortcode for example name [is_logged_in] and use it like this

    [is_logged_in][adverts_add][/is_logged_in]

    Depending on user being logged in or not your shhortcode would display error message or execute [adverts_add].

    How to write a shortcode like this is described here https://codex.www.ads-software.com/Shortcode_API

    Thread Starter Iurie Malai

    (@flegmatiq)

    So, this is the [is_logged_in] shortcode function. It will display a message for not logged in users or the Add adverts page/form for logged in users. If registration is disabled the link ‘Register’ will not be displayed.

    // The usage: [is_logged_in][adverts_add][/is_logged_in]
    
    // Hide Add Adverts page from not logged in users shortcode
    function is_logged_in_shortcode( $atts , $content = null ) {
    
        // check if user is not logged in
        if ( !get_current_user_id() ) {
            $permalink = get_permalink();
            $message = __('Attention! Only logged in users can access this page.', 'wpadverts-custom-settings');
            // check if registration is disabled
            if ( !get_option( 'users_can_register' )) {
                $message .= __(' <a class="button" href="%1$s">Login</a>', 'wpadverts-custom-settings');
            } else {
                $message .= __(' <a class="button" href="%1$s">Login</a> or <a class="button" href="%2$s">Register</a>', 'wpadverts-custom-settings');
            }
            $parsed = sprintf($message, wp_login_url( $permalink ), wp_registration_url( $permalink ) );
            $message = adverts_flash( array( "error" => array( $parsed ) ) );
            return $message;
        }
        return do_shortcode( $content );
    }
    add_shortcode( 'is_logged_in', 'is_logged_in_shortcode' );
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Hide the form if registration is not allowed’ is closed to new replies.