我有一个php文件我想要Wordpress身份验证,因此您只能在登录后访问该页面。我不希望人们通过输入来查找该文件路径。
目前我正在运行WAMP并使用本地主机。我把文件放在"模板"在Wordpress文件夹中的文件夹。
我需要添加哪些代码才能使用Wordpress进行登录保护?
<?php
include("connection.php");
$query = "SELECT * FROM category";
$result = mysqli_query($connection, $query);
$output = '';
while($row = mysqli_fetch_array($result))
{
$output .= '<option value="'.$row["category_id"].'">'.$row["category_name"].'</option>';
}
?>
<html>
<head>
<title>Mankato State University - Inquriy Tools</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-bootgrid/1.3.1/jquery.bootgrid.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-bootgrid/1.3.1/jquery.bootgrid.js"></script>
<style>
body
{
margin:0;
padding:0;
background-color:#f1f1f1;
}
.box
{
width:1270px;
padding:20px;
background-color:#fff;
border:1px solid #ccc;
border-radius:5px;
margin-top:25px;
}
</style>
</head>
<body>
<div class="container box">
<a href="https://www.mnsu.edu/"><h1 align="center"><img src="msu-logo-large.jpg" align="center"></h1></a>
<br />
<br />
<div align="right">
<button type="button" id="add_button" data-toggle="modal" data-target="#productModal" class="btn btn-info btn-lg">Add</button>
<button onclick="location.href='http://localhost/wordpress/wp-login.php?loggedout=true'" class="btn btn-info btn-lg type="button">Logout</button>
<button onclick="location.href='http://localhost/wordpress/wp-admin/about.php'" class="btn btn-info btn-lg type="button">Settings</button>
</div>
<div class="table-responsive">
<table id="product_data" class="table table-bordered table-striped">
<thead>
<tr>
<th data-column-id="FORM_ID" data-type="numeric">ID</th>
<th data-column-id="worker_name">Worker</th>
<th data-column-id="category_name">Category</th>
<th data-column-id="student_name">Student</th>
<th data-column-id="tech_id">TechID</th>
<th data-column-id="course_wanted">Course</th>
<th data-column-id="email">Email</th>
<th data-column-id="phone">Phone</th>
<th data-column-id="date">Date</th>
<th data-column-id="comments">Comments</th>
<th data-column-id="status">Status</th>
<th data-column-id="commands" data-formatter="commands" data-sortable="false">Commands</th>
</tr>
</thead>
</table>
</div>
</body>
</html>
<script type="text/javascript" language="javascript" >
$(document).ready(function(){
$('#add_button').click(function(){
$('#product_form')[0].reset();
$('.modal-title').text("Add Student");
$('#action').val("Add");
$('#operation').val("Add");
});
var productTable = $('#product_data').bootgrid({
ajax: true,
rowSelect: true,
post: function()
{
return{
id: "b0df282a-0d67-40e5-8558-c9e93b7befed"
};
},
url: "fetch.php",
formatters: {
"commands": function(column, row)
{
return "<button type='button' class='btn btn-warning btn-xs update' data-row-id='"+row.FORM_ID+"'>Edit</button>" +
" <button type='button' class='btn btn-danger btn-xs delete' data-row-id='"+row.FORM_ID+"'>Delete</button>";
}
}
});
$(document).on('submit', '#product_form', function(event){
event.preventDefault();
var category_id = $('#category_id').val();
var worker_name = $('#worker_name').val();
var student_name = $('#student_name').val();
var tech_id = $('#tech_id').val();
var course_wanted = $('#course_wanted').val();
var email = $('#email').val();
var phone = $('#phone').val();
var comments = $('#comments').val();
var form_data = $(this).serialize();
if(category_id != '' && worker_name != '' && student_name != '' && tech_id != '' && course_wanted != '' && email != '' && phone != '' && comments != '')
{
$.ajax({
url:"insert.php",
method:"POST",
data:form_data,
success:function(data)
{
alert(data);
$('#product_form')[0].reset();
$('#productModal').modal('hide');
$('#product_data').bootgrid('reload');
}
});
}
else
{
alert("All Fields are Required");
}
});
$(document).on("loaded.rs.jquery.bootgrid", function()
{
productTable.find(".update").on("click", function(event)
{
var FORM_ID = $(this).data("row-id");
$.ajax({
url:"fetch_single.php",
method:"POST",
data:{FORM_ID:FORM_ID},
dataType:"json",
success:function(data)
{
$('#productModal').modal('show');
$('#category_id').val(data.category_id);
$('#worker_name').val(data.worker_name);
$('#student_name').val(data.student_name);
$('#tech_id').val(data.tech_id);
$('#course_wanted').val(data.course_wanted);
$('#email').val(data.email);
$('#phone').val(data.phone);
$('#comments').val(data.comments);
$('#status').val(data.status);
$('.modal-title').text("Edit Product");
$('#FORM_ID').val(FORM_ID);
$('#action').val("Edit");
$('#operation').val("Edit");
}
});
});
});
$(document).on("loaded.rs.jquery.bootgrid", function()
{
productTable.find(".delete").on("click", function(event)
{
if(confirm("Are you sure you want to delete this?"))
{
var FORM_ID = $(this).data("row-id");
$.ajax({
url:"delete.php",
method:"POST",
data:{FORM_ID:FORM_ID},
success:function(data)
{
alert(data);
$('#product_data').bootgrid('reload');
}
})
}
else{
return false;
}
});
});
});
</script>
<div id="productModal" class="modal fade">
<div class="modal-dialog">
<form method="post" id="product_form">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Add Student</h4>
</div>
<div class="modal-body">
<label>Select Request Type</label>
<select name="category_id" id="category_id" class="form-control" required>
<option value="">Select Request Type</option>
<?php echo $output; ?>
</select>
<br />
<label>Enter Worker Name</label>
<input type="text" name="worker_name" id="worker_name" class="form-control" placeholder="Worker first and last name" required/>
<br />
<label>Enter Student Name</label>
<input type="text" name="student_name" id="student_name" class="form-control" placeholder="Student first and last name" required/>
<br />
<label>Enter TechID</label>
<input type="number" name="tech_id" id="tech_id" class="form-control" placeholder="Tech id" required/>
<br />
<label>Enter Course</label>
<input type="text" name="course_wanted" id="course_wanted" class="form-control" placeholder="Section and course id" required/>
<br />
<label>Enter Email</label>
<input type="email" name="email" id="email" class="form-control" placeholder="Email" required/>
<br />
<label>Enter Phone</label>
<input type="text" name="phone" id="phone" class="form-control" placeholder="Phone" required/>
<br />
<label>Enter Comment</label>
<input type="text" name="comments" id="comments" class="form-control" placeholder="Put N/A for blank"/>
<br />
<label>Enter Status</label>
<input type="text" name="status" id="status" class="form-control" placeholder="Auto added"/>
<br />
</div>
<div class="modal-footer">
<input type="hidden" name="FORM_ID" id="FORM_ID" />
<input type="hidden" name="operation" id="operation" />
<input type="submit" name="action" id="action" class="btn btn-success" value="Add" />
</div>
</div>
</form>
</div>
</div>
答案 0 :(得分:0)
请检查此解决方案 - Click Here
设置你的定义
define( 'WP_USE_THEMES', false );
define( 'COOKIE_DOMAIN', false );
define( 'DISABLE_WP_CRON', true );
加载WordPress核心
require("/var/www/yourdomain.com/htdocs/wp-load.php");
验证,验证,设置Cookie
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
} else {
$creds = array();
// If you're not logged in, you should display a form or something
// Use the submited information to populate the user_login & user_password
$creds['user_login'] = "";
$creds['user_password'] = "";
$creds['remember'] = true;
$user = wp_signon( $creds, false );
if ( is_wp_error( $user ) ) {
echo $user->get_error_message();
} else {
wp_set_auth_cookie( $user->ID, true );
}
}
最后检查&amp;使用会话
if ( !is_wp_error( $user ) ) {
// Success! We're logged in! Now let's test against EDD's purchase of my "service."
if ( edd_has_user_purchased( $user->ID, '294', NULL ) ) {
echo "Purchased the Services and is active.";
} else {
echo "Not Purchased";
}
}