简体   繁体   English

在php页面中要包含哪些内容以使用wordpress的功能?

[英]What to include in php page to use wordpress's functions?

I have a wordpress website and I want to create a page that also has javascript and upon a certain user action the javascript calls the server with an AJAX request. 我有一个wordpress网站,我想创建一个也包含javascript的页面,并且在某些用户操作后,javascript会使用AJAX请求调用服务器。 This is a GET request to a php script I created. 这是对我创建的php脚本的GET请求。 Since I extended wordpress with my plugin I put this php in my plugin's folder. 由于我使用插件扩展了wordpress,因此将此php放入插件的文件夹中。

The problem is that from this php script I want to access everything that wordpress offers, eg the database access, but I do not know how. 问题是,我想从此php脚本访问wordpress提供的所有内容,例如数据库访问权限,但我不知道如何操作。

What do I have to include in this php file in order to access the functions offered by wordpress? 为了访问wordpress提供的功能,我必须在此php文件中包括什么? I wanted to use database access so I included the wp-db.php file and declared the global wpdb variable, but it did not help. 我想使用数据库访问权限,所以我包含了wp-db.php文件并声明了全局wpdb变量,但是它没有帮助。

Can anyone tell me how to accomplish this? 谁能告诉我如何做到这一点?

Thanks in advance! 提前致谢!

Wordpress has it's own build in ajax: http://codex.wordpress.org/AJAX_in_Plugins WordPress在Ajax中拥有自己的构建: http : //codex.wordpress.org/AJAX_in_Plugins
Use this instead of your own ajax script. 使用它代替您自己的ajax脚本。

In this ajax script you can use all WP functions. 在此ajax脚本中,您可以使用所有WP函数。 It will also make your plugin more like wordpress standards. 它还将使您的插件更像Wordpress标准。

As recommended in he WordPress Codex itself. 正如他在WordPress Codex本身中所建议的那样。

<?php 
/* Short and sweet */
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');
?>

By using the blog header no database queries are executed by default, you have to supply the get_posts that will fetch what-ever articles you need based on your application's parameters: 通过使用Blog标头,默认情况下不执行数据库查询,您必须提供get_posts ,它们将根据应用程序的参数获取所需的任何文章:

<?php $posts = get_posts('numberposts=10&order=ASC&orderby=post_title'); ?>
<?php foreach ($posts as $post) : start_wp(); ?>
    <?php the_date(); echo "<br />"; ?>
    <?php the_title(); ?>    
    <?php the_excerpt(); ?> 
<?php endforeach; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM