• I currently have the below code:
    wp_mail( get_option( 'admin_email' ), "{$user->user_login}", "{$user->user_login} has logged in from a different computer." );

    and I’m trying to add the Shortcode:
    [whatsmyip ip='yes' country='yes']

    My guess is that I need to add the below code somewhere where it needs to be added in the above code as I keep getting errors.
    echo do_shortcode( '[whatsmyip ip='yes' country='yes']' );

    Any ideas?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Your question is not properly explained. Please give an example if possible sir.

    Moderator bcworkz

    (@bcworkz)

    I think I get it despite the poor explanation. The shortcode expansion should be included in the email message.

    You don’t use echo in this situation because that sends content to a browser. Instead, collect the do_shortcode() return into a variable, then use the variable in the email message.

    $user_ip = do_shortcode( "[whatsmyip ip='yes' country='yes']" );
    wp_mail( get_option( 'admin_email' ), "{$user->user_login}", "{$user->user_login} has logged in from a different computer, IP: $user_ip" );

    You also cannot use the same quote character inside a string that is used to delimit that string. This is incorrect:
    do_shortcode( '[whatsmyip ip='yes' country='yes']' );
    Either use double quotes to delimit “yes” or delimit the entire string, but not both at the same time. Same for single quotes.

    Thread Starter ajiaim

    (@ajiaim)

    Sorry for the poor explanation.
    Basically what I’m trying to do is get WordPress to notify me once multiple users sign in using a single user account.

    I’ve got the notification bit working but I would like to include the IP of the user in the email.

    I have the plugin “whats-my-ip”
    https://www.ads-software.com/plugins/whats-my-ip/

    So I thought the easiest way is to use the shortcode from the plugin to send the IP address along with the email.

    Shortcode:
    [whatsmyip ip=’yes’ country=’yes’]

    I followed bcworkz instructions but the email i get is:

    has logged in from a different computer, IP: <span class=”whats-my-ip”>
    <span class=”ip”></span>

    <span class=”country”></span>

    </span>

    Moderator bcworkz

    (@bcworkz)

    Well, our code snippet is clearly working correctly. It appears the place where the IP plugin gets its data is no longer valid by the time our code executes. I think you need to move the $user_ip = do_shortcode() line to where ever the code is that decides the user is using a new IP. Depending on the scope of the various bits of code you may need to declare $user_ip global or manage the scope in some other manner.

    By chance the code placement is already adjacent to the new IP logic, as a workaround you can easily get the user’s IP from $_SERVER['REMOTE_ADDR'], but the IP’s country is not so simple. There’s many sites that will return a country given a particular IP, they likely have some sort of API your server could access. This is likely what the plugin does.

    Thread Starter ajiaim

    (@ajiaim)

    Many thanks for the explanation bcworkz.
    I really really appreciate it!

    I have one last question about $_SERVER[‘REMOTE_ADDR’]

    In the code I provided:

    $user_ip = do_shortcode( "[whatsmyip ip='yes' country='yes']" );
    wp_mail( get_option( 'admin_email' ), "{$user->user_login}", "{$user->user_login} has logged in from a different computer, IP: $user_ip" );

    How do I implement $_SERVER[‘REMOTE_ADDR’] into that?

    Sorry for my lack of programming skills…

    Moderator bcworkz

    (@bcworkz)

    Well, in theory you could just do this:

    $user_ip = $_SERVER['REMOTE_ADDR'];
    wp_mail( get_option ...

    And it’s certainly worth a try, but I fear this will fail for the same reason the shortcode doesn’t work. And of course this will not tell you what country the IP is from, but getting just the IP is better than nothing.

    On the chance this does not work, where is this code placed … what context causes it to execute?

    you an also use shortcode plugin to create your shot code for simple functions

    https://www.ads-software.com/plugins/shortcodes-ultimate/

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Adding Shortcode to PHP File in WordPress’ is closed to new replies.