Get someone elses public instagram feed - wordpress

Lots of widgets and snippets out there for displaying your own feed but cant seem to find anything for displaying someone else's public feed.
Need this for a project I'm working on.
http://instafeedjs.com claims you can get images based on a tag which would almost suffice but it doesn't work so I assume the Instagram API has since been refined.
Why would Instagram restrict access to what is available publicly?

Instagram looks like they did restrict their API but you can still get user info when a request is made with the url below:
https://www.instagram.com/{username}/?__a=1
E.g: This url will get all information about a user who's username is therock
https://www.instagram.com/therock/?__a=1
This was my solution, specifically this applies to wordpress:
<?php
// The instagram user eg: elonmusk
$instagramId = get_field('instagram_id');
if($instagramId) {
$request = wp_remote_get( 'https://www.instagram.com/' . $instagramId . '/?__a=1' );
if( is_wp_error( $request ) ) { return false; // Something }
$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body, true );
// Get the actual user ID eg: 3602415960
$userID = $data["user"]["id"];
}?>
And then finally use the userID in instafeedjs to display the feed.
<div id="instafeed">
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/instafeed.js/1.4.1/instafeed.min.js"></script>
<script type="text/javascript">
var php_var = "<?php echo $userID; ?>";
var userFeed = new Instafeed({
get: 'user',
userId: php_var,
clientId: '*******Enter yours******',
accessToken: '4622774.7cbaeb5.ec8c5041b92b44ada03e4a4a9153bc54',
template: '<img src="{{image}}" style="width:25%;"/>',
sortBy: 'most-recent',
limit: 24,
links: false
});
userFeed.run();
</script>
http://instafeedjs.com
https://pippinsplugins.com/using-wp_remote_get-to-parse-json-from-remote-apis/
I really hope this helps someone else.

Related

Wordpress Snapchat: Pass Email data

I have added the Snap Pixel Code in the head, which includes: 'snaptr('init', 'ID', { 'user_email': 'INSERT_USER_EMAIL' });'
I now have to pass on the email variable but don't know how to do this.
I am using Contact Form 7 in Wordpress.
From your question I understand that you're looking to integrate the Snap Pixel into Wordpress.
You can use something like:
<?php
$email = "test#test.com"; //your mail address
?>
<script>
snaptr('init', 'ID', { 'user_email': <?php echo $email; ?> });
</script>
You can also use a Wordpress Snap Pixel plugin, see for example: https://wordpress.org/plugins/snap-pixel/#installation

I need help generating a custom URL based on the user ID

I need help on how generate a unique URL based on a users ID for example
var htmlstring = "https://www.google.com" + "?" + <?php get_userdata( $userid ); ?>
This is the JS I have that creates a hyperlink in the html:
Click <script type="text/javascript">
function zs_open_window(url, height, width){var leftPos = 0;var topPos = 0;if(screen){leftPos = (screen.width - width) / 2;topPos = (screen.height - height) / 2;window.open(url, null, 'width='+width+',height='+height+',left='+leftPos+',top='+topPos+', toolbar=0, location=0, status=1, scrollbars=1, resizable=1');}}</script>HERE
I need to get the Wordpress username as a JS variable so I can modify the href to be href="https://survey.zohopublic.com/zs/NbB3YM?USERNAME".
I know how to modify the href, I just don't know how push a username into a JS variable?
Ben
You could setup a global variable on all pages that holds that information by adding this to your header.php file just above the <?php wp_head(); ?> line.
<?php
// Gets the current user.
$current_user = wp_get_current_user();
// Checks if the visitor is logged in.
if ( $current_user->ID !== 0 ) {
// Output JS
?>
<script type="text/javascript">
var htmlstring = "https://survey.zohopublic.com/zs/NbB3YM?username=<?php echo $current_user->user_login ?>";
</script>
<?php
} // End if
?>
You can then use htmlstring in any of your other JS functions or code. I also added the username= parameter as you might need that so you can pass it to something else. If you don't, just remove it.

How to add verified badge in front of author name across wordpress blog

Good Day,
I have been trying to add verification badge to WordPress users but I no success. I tried using administrator to do the testing by checking if user has role administrator and trying to enter code here update_user_meta (display_name) but I wasn't able to add icons to the display name.
I have as well tried creating a custom field in user profile called verify_user (text field) .... In which I entered the value "verified" and saved ... I have been searching for hooks to use but haven't seen one. I'm not sure which hook/filter to use here
Please is there any solution to adding this verification icon to author's display name in which when the word "verified" is entered into the custom field created in user profile or any other approach available (e.g if user has specific role). I don't mind if the little structure I wrote above would be changed.
Thanks
I was able to get a solution which worked exactly as i wanted. For anyone who might have same task to tackle;
function add_verification_bagdge_to_authors($display_name) {
global $authordata;
if (!is_object($authordata))
return $display_name;
$icon_roles = array(
'administrator',
'verified_author',
);
$found_role = false;
foreach ($authordata->roles as $role) {
if (in_array(strtolower($role), $icon_roles)) {
$found_role = true;
break;
}
}
if (!$found_role)
return $display_name;
$result = sprintf('%s <i title="%s" class="fa fa-check-circle"></i>',
$display_name,
__('This is a Verified Author', 'text-domain-here')
);
return $result;
}
add_filter( 'the_author', 'add_verification_bagdge_to_authors' );
The way i was able to tackle this was to create a user role called Verified Author (using add_role() function from Wordpress Codex ), which will be the role assigned to verified author across the website. All Users with Admin Role are automatically Verified while user role has to be switched from either contributor/Author role to Verified Author.
The above code was able to do 98% of the task but when a verified author / administrator comments, their display name doesn't show the verified badge. I was able to use the below code to fix that (combining the code with a shortcode).
function my_comment_author( $author = '' ) {
// Get the comment ID from WP_Query
$comment = get_comment( $comment_ID );
if ( ! empty($comment->comment_author) ) {
if (!empty($comment->user_id)){
$user=get_userdata($comment->user_id);
$author=$user->display_name.' '. do_shortcode('[this_is_verified-sc]'); // This is where i used the shortcode
} else {
$author = $comment->comment_author;
}
} else {
$author = $comment->comment_author;
}
return $author;
}
add_filter('get_comment_author', 'my_comment_author', 10, 1);
Shortcode to return the Verified Badge if user is admin or verified author
function add_verification_bagdge_to_authors_sc($display_name) {
global $authordata;
if (!is_object($authordata))
return $display_name;
$icon_roles = array(
'administrator',
'verified_author',
);
$found_role = false;
foreach ($authordata->roles as $role) {
if (in_array(strtolower($role), $icon_roles)) {
$found_role = true;
break;
}
}
if (!$found_role)
return $display_name;
$result = sprintf('%s <i title="%s" class="fa fa-check-circle"></i>', $display_name, __('This is a Verified Author', 'text-domain-here') );
return $result;
}
add_shortcode( 'this_is_verified-sc', 'add_verification_bagdge_to_authors_sc' );
Please Note: Not all Magazine theme are properly coded / have a hard coded Block and module elements, so the verified badge might not work on their block / module element. I experienced this during the whole website design process, so we had to change up theme and the only modification i had to make to the new theme was to remove the html escape added to their block/module code, so that the icon can be rendered. i.e. in their module/block code they had something like this esc_html( the_author() ) and i removed the esc_html to have just the_author() an it all worked.
Its not really a straight forward solution but that is how i was able to tackle the task without using a plugin. Just add the codes to your functions.php file and that's it. I hope it helps someone.
Thanks to Kero for pointing me in the right direction, providing the code i was able to work with and manipulate

How to notify through email in wordpress

How to notify through email in wordpress when visitor clicks on a link that this link was pressed and/or user ip, city and country was this?
I have given the link a class 'email-link'.
something like this should get you started
// functions.php
// HTML form markup
function mail_form_stuff() {
echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post">';
echo '<p><input type="submit" name="send_the_email_to_admin" value="Click this button"></p>';
echo '</form>';
}
// make sure the email will allow for html -- not just plain text
add_filter( 'wp_mail_content_type', 'send_the_email_to_admin_content_type' );
function send_the_email_to_admin_content_type() {
return 'text/html';
}
// if the button is clicked, send the email
function deliver_mail_link_stuff() {
$admin_email = get_option( 'admin_email' );
if ( isset($_POST["send_the_email_to_admin"]) ) {
$_user = wp_get_current_user();
$_name = esc_html( $_user->user_firstname );
$_email = esc_html( $_user->user_email );
$to = $admin_email;
$subject = "Some person clicked my link";
$message = 'Hey this person clicked on that button'.'<br>';
$message .= "the persons email address was: $_email";
$headers[] = "From: $_name <$_email>" . "\r\n";
// $headers[] = "Bcc: John Smith <jsmith#gmail.com>" . "\r\n";
// If everything worked -- display a success message
if ( wp_mail( $to, $subject, $message, $headers ) ) {
echo '<p>sent</p>';
} else {
echo 'An unexpected error occurred';
}
// reset wp_mail_content_type
remove_filter( 'wp_mail_content_type', 'send_the_email_to_admin_content_type' );
}
}
function add_short_code_stuff() {
ob_start();
deliver_mail_link_stuff();
mail_form_stuff();
return ob_get_clean();
}
add_shortcode( 'EMAIL_BUTTON', 'add_short_code_stuff' );
this will add a short code for you
you can then call the shortcode in your theme with
echo do_shortcode('[EMAIL_BUTTON]');
You can't do this as a direct consequence of WordPress since a link is HTML and has no correlation to or integration with anything controlled or processed directly by WordPress. WordPress is merely a structure/processes through which you can add info to a database and then later retrieve it.
If you question is actually meant in a more generic and not specifically wordPress sense, then the answer is any number of ways. You could for example create some JQuery or JS that would add that info everytime a link was clicked. But, you would need to interact with the page headers to try and get all the required info.
But, why bother doing that when a free & arguably market leading tool that does this is already available?
The logical process to this is to use Google Analytics (or a rival tool) as this already collects this kind of info with very little work required to set it up. If you want more specific "event" triggered data (eg a link is clicked), then you can also do this fairly easily too.
https://analytics.google.com
UPDATE
In your comment, you clarify that you want a real time email to be sent to you when a link is clicked, and thus analytics isn't going to fit the bill. In that case you will need to do some work using JQuery & AJAX.
In simple terms you'll need to do something this:
1) Create some JQuery to intercept the url of the link clicked
2) Pass the link (and header info) to a function via an AJAX call
3) Process the header data / send the email
4) Redirect user to the url passed from the link
Here's a tutorial on creating a simple AJAX process in WordPress: https://www.makeuseof.com/tag/tutorial-ajax-wordpress/

BuddyPress / Wordpress Get dynamic user ID

I'm trying to make a function that overrides the current users avatar URL with a custom URL that is saved in their user meta. So far the function works except that I'm stumped trying to figure out how to have the function grab the user ID of the user buddypress/wordpress is getting an avatar for.
The code so far looks like this:
// return new URL for avatar
function wpse_49216_my_new_avatar_url() {
// get user_id of user bp/wp is getting avatar for
$user_id = bp_get_member_user_id();
// get avatar name from above user's meta
$avatar_choice = get_user_meta($user_id, "avatar_choice", true);
if($avatar_choice) {
return 'path_to_avatars/'.$avatar_choice.'.png';
} else {
return 'path_to_avatars/default-avatar.png';
}
}
add_filter( 'bp_core_fetch_avatar_url', 'wpse_49216_my_new_avatar_url' );
// Replace image src="" with the new avatar URL
function wpse_49216_filter_bp_avatar( $html ) {
return preg_replace( '/src=".+?"/', 'src="' . wpse_49216_my_new_avatar_url() . '"', $html );
}
add_filter( 'bp_core_fetch_avatar', 'wpse_49216_filter_bp_avatar' );
The main problem is if I set the $user_id to the current logged in user's ID, then all avatars will be load the avatar of the current logged in user. And bp_get_member_user_id() only works on member pages. I need something that works universally. Does any Wordpress/Buddypress expert have any idea how I can get the correct user ID?
I need something that works universally
If you look at the function you are filtering, you'll see that it depends on knowing 'where' you are in BP. Since it is driven by context, there is no universal approach.
You can user below functiona for the Global userID:
bp_displayed_user_id()

Resources