I am building an external website for a client using the bootstrap 4 framework. and they have a Wordpress with all of their data linked, music and videos, etc stored in it. The Wordpress acts as just a management system for their content. I will be building a separate webpage/site for them to present to their users that will allow users to sign up and subscribe for the mailing list and updates etc.... SO having to never use an API ever. I was wondering what do I need to place in the index.php file to call upon the wordpress api from https://wp.example.com. Does it need to go into the head section or right before the closing body tag?
I would like to use the WordPress Api to GET or echo the current user's avatar and username. to include inside the bootstrap header navbar.
<?php
global $current_user;
get_currentuserinfo();
echo get_avatar( $current_user->ID, 64 );
?>
<?php global $current_user; wp_get_current_user(); ?>
<?php if ( is_user_logged_in() ) {
echo 'Welcome Back, ' . $current_user->user_login . "\n"; echo 'User display name: ' . $current_user->display_name . "\n"; }
else { wp_loginout(); } ?>
and finally last but not least I would like to use the wordpress api to check if a user is logged in/out to display/hide a bootstrap button the will contain an exclusive menu. I'm pretty new to this but I think its something like this
<?php global
if ( is_user_logged_in() ) {
// <button type="button" class="btn btn-outline-primary">Menu</button>
} else {
<button type="button" class="btn btn-outline-primary">SignUp/Login</button>
}
?>
BUt I Dont know exactly how to call or request, head or include or whatever it is that is required to use the api from the https://wp.example.com installation in the html or where to place it ie. , , to make these functions work. Please Help
Definitely follow cabrerahector's advice and look at the documentation for the WordPress API...that is the best place to start.
To answer your question...the only reason to use the WP REST API is when you do not want to use WordPress for handling the front-end of your website.
So... you will not be using your 'Theme' folder except for index.php, functions.php and style.css.
You will also probably want to put your WordPress install in a sub-domain and run your front-end from the primary domain.
I put the following code in my theme's index.php
<script type="text/javascript">window.location = 'http://admin.example.com/wp-admin/';</script>
check out this article Using WordPress as a Headless CMS
Also, the WP REST API is limited...I have had to add several custom endpoints because some data is not included out-of-the-box.
You can get 'user' data by using the following endpoint
GET /wp/v2/users/<id>
REST API Handbook/users
Related
I am using divi theme in wordpress for my site. and i add one header using page builder. I want that only logged in user can clicked on shop now button and if they are not logged in then they will redirect to login page
You are asking about quite basic functionality of the WordPress API:
boolean is_user_logged_in()
If you are modifying the theme directly or have enabled a plugin that lets you use PHP-code on your pages and posts, you can use this Wordpress-/PHP-function in combination with a PHP if-statement to check whether the current user is logged in and, in case he or she is, add really anythingto your page. Ok, except for a living unicorn.
<?php if ( is_user_logged_in() ) : ?>
Shop Now
<?php endif; ?>
You said if the current user of your web-page is not logged in, he or she is supposed to be redirected to the login-page. The else-statement is probably what you are looking for. And, if you are brave enough, try not to hard-code the login link here by making use of the WordPress API-function wp_login_url():
<?php if ( is_user_logged_in() ) : ?>
Shop Now
<?php else: ?>
Log In to shop
<?php endif; ?>
Remember, if you want to use this code on your wordpress pages, articles, widgets, whatever..., you have to install a plugin that lets you use PHP there. Also, this code is not completly free of avoidable repetitions for the sake of making the examples easy to understand. It should be easy to figure out how to optimize the code.
I have a site that is going to be used by Real estate agents, Brokers, and apt. hunters to locate and lease apartments. I need the brokers to be able to add showing instructions to their listing that agents can view but not edit and the public cannot view.
I am using a "real estate" theme that has gotten me 2/3 of the way there but this added functionality that I require doesn't seem to be able to be plugged into the theme with... a plug in. I've tried Advanced custom fields, members, and numerous other role, and custom field plug ins. I was convinced that this project would or could be supported by some existing plugin or widget but am now not so sure.
If anyone has experience with this please throw me a bone. I dont expect code examples but just a pointer in the right direction.
Thanks in advance!
Assuming the theme uses custom roles for brokers/agents etc, why not add a custom field to the listing editor screen, then use an if current_user_can capability check in the template to display it.
e.g
<?php if( current_user_can( ' edit_listing ' ) : ?>
<?php echo get_field( ' instructions ' ); ?>
<?php endif; ?>
I'd like to make all my articles publicly accessible but in order to view the index page, a user must first register with the site. Is this possible?
To implement in a particular page you can use the following:
To limit the access to one particular page (or some pages) so that only logged in users can reach it, I make a new page template for that purpose and then I put this bit of code in the beginning of the template (right after the ):
<?php if ( !is_user_logged_in() ) {
nocache_headers();
header("HTTP/1.1 302 Moved Temporarily");
header('Location: ' . get_settings('siteurl') . '/wp-login.php?redirect_to=' . urlencode($_SERVER['REQUEST_URI']));
header("Status: 302 Moved Temporarily");
exit();
}
?>
the above example is taken from here
Additionally, you can use any one of the several wordpress plugins out there. One example is here. I have used this plugin before and it works pretty well. There are other similar plugins with varying features. A simple google search would give you lots of options.
I'm doing some work on an existing site that is based on the Wordpress theme, but uses about 15 plugins (Including Buddypress). One in particular is the WP Sliding Login|Dashboard plugin, which has a link to the user's activity feed. I found the code that creates that link in the wp-sliding-login-dashboard.php file:
<?php
if ( is_user_logged_in() ) {
global $current_user;
$username = $current_user->user_login;
echo ' <li>Activity Feed</li>';
}
?>
I want to use this code to send the user to the same location, but using the a link at the top of the home page. Unfortunately, the home page links are all created using Wordpress menus, which as far as I can tell, only allow for the use of static links attached to existing pages.
Do I create a dummy page to link to that exits only to execute the above code? Is that even possible? Picture a five-year-old trying to read Shakespeare, and you have an idea of my ability as a coder, so feel free to engage me as such - i.e. if you say, "oh just create a scoping function instead of creating a global function", i would stare at you drooling and confused.
Images for clarity: The sliding login menu (WP-Sliding Login|Dashboard Plugin), showing the target URL in the status bar as www.ferrignofit.com/members/FerrignoFit/activity/ (the current logged in user is FerrignoFit):
http://i.imgur.com/NPvmCXU.jpg
The main page Wordpress-based menu, which i want to go to the above URL, but is currently going to www.ferrignofit.com/activity/, a different page:
http://i.imgur.com/dIiFpDC.jpg
So here's a jQuery solution for this specific issue. From seeing the images you have, what you want to do is target a specific anchor in your dynamic WordPress menu.
As you may be aware, you can create custom links for the WordPress menu function... it simply lets you set a label and a URL. You should create such item and just give it a hash for the URL for now.
Now set a class for that specific menu item so you can have a nice handle for jQuery to target it (otherwise you can use the dynamic class that WordPress creates for each specific menu-item).
One you have a class set or you know what you need to target then you can add this block of code before your menu.
<?php if (is_user_logged_in()){ ?>
<script>
$(document).ready(function(e) {
var targetNav = $('li.customClassName a');
var userName = '<?php $current_user = wp_get_current_user(); echo $current_user->display_name;?>';
var userUrl = 'http://www.mywebsitename.com/members/'+ userName +'/activity/';
targetNav.attr('href',userUrl);
});
</script>
<?php } else { ?>
<script>
$(document).ready(function(e) {
var targetNav = $('li.customClassName a');
targetNav.attr('href','http://www.stackoverflow.com');
});
</script>
<?php } ?>
Please not that I am using PHP to get the current username in WordPress, after I get the username and I store it in the userName variable I use it in the userUrl to set it with the path that I want.
On another note, I'm using is_user_logged_in() so you have the option of making the link something else if the user is in fact not logged in. So with this if statement one of the two blocks of code will be output by PHP.
As soon as my two variables are set, I simply target my menu item with jQuery and modify the href attribute to replace the hash with the path and dynamic username.
Though this is not very good practice to mix JS and PHP, I'd like to learn myself here what other solutions someone can suggest so I'm posting this as a solution for your very specific issue.
I tested this code with my own WordPress site and it works, just remember that this code NEEDS to be in a PHP file otherwise the PHP used in the <script> tags won't mean anything to the server if it's in a .js file.
I'd like to implement a custom post retrieval page in wordpress. Basically, I'm using AJAX to call this page that will be passed a post ID and retrieve certain data from that post.
Note: please don't mistake this as a template question. I do not want a template for one single page -- I am looking to make this page query multiple different posts based on postID and return certain data from that post.
So I tried creating a page
<?php
$args=array(
'p'=>'77'
);
$friends = new WP_Query($args);
?>
<?php if ($friends->have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php else: ?>
<p>Sorry, no posts are available.</p>
<?php endif; ?>
But this does not work since it is not loading in the wp functions to handle the query.
Thanks in advance for any help!
You have to include the wp-blog-header.php file. If the page you are creating is in your theme's folder then you would put something like this at the top of your code
<?php require_once ('../../../wp-blog-header.php');?>
I think I guess what you are trying to do, and it sounds like you are going about it the wrong way. Do not make a 'new page' in the admin interface. What you want to do is serve up a file (JSON, XHTML fragment, whatever) to your Javascript and include in it WP data, right? I know that problem, having used it in some of my plugins.
There are two techniques:
(1) This is what you need to do: make a new plugin (just a loose php file in wp-plugins with the same format header as the other plugins in there). Write your function along these lines:
function mydatapage(){
if (...$_SERVER['REQUEST_URI'] == the one I am using ...) {
$args=array(
'p'=>'77'
);
$friends = new WP_Query($args);
if ($friends->have_posts()) :
the_post();
the_title();
the_content();
else:>?
<p>Sorry, no posts are available.</p>
<?php endif;
die();
} //else do nothing and leave WP to serve the page normally
}
//Crucially:
add_action('init', 'mydatapage');
What that does is do a lookup when pages are loaded to see if the url matches the one you want to hijack and use to send your custom data. If it is, you send the data/file/whatever you feel like and exit (die).
Give a shout if you want more detailed syntax. It is a bit messy, but works well.
(2) Directly call your plugin file. WP will only handle files that do not already exist, and leave the rest to Apache. That means you can make a plugin file and call that directly using the .../wp-plugin/myfile.php url. You would need to include some of the WP core files to get things like WP_Query to work. It is marginally more fragile a method.