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.
This is a GET request to a php script I created. Since I extended wordpress with my plugin I put this php in my plugin's folder.
The problem is that from this php script I want to access everything that wordpress offers, e.g. the database access, but I do not know how.
What do I have to include in this php file in order to access the functions offered by wordpress? 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.
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
Use this instead of your own ajax script.
In this ajax script you can use all WP functions. It will also make your plugin more like wordpress standards.
As recommended in he WordPress Codex itself.
<?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:
<?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; ?>
Related
I am trying to get latest wordpress posts on a static page outside the wordpress. I'm trying following code but it does not work (ie. I do not get any output).
<?php
$config_file = $_SERVER['DOCUMENT_ROOT'].'/wp/wp-config.php';
include($config_file);
$postlist = get_posts('numberposts=5');
foreach ($postlist as $post) : ?>
<p><?php the_title(); ?></p>
<?php endforeach; ?>
The wp-config file only stores those variables for use in other scripts. It doesn't define any of the wordpress functions. I believe what you are looking for would be something like this.
define('WP_USE_THEMES', false);
require('/server/path/to/your/wordpress/site/htdocs/blog/wp-blog-header.php');
After that, you should be able to use wordpress functions.
I have some custom post types, such as 'review'. I can't seem to find out how to make a section (e.g., www.mysite.com/reviews/) which works like the blog home page, but lists reviews instead of posts (with pagination and everything). I'd like to use a separate template for it too.
Create a page called reviews and then create a new template in your theme folder called page-reviews.php. Add all the necessary elements to the template and include a query_posts in front of your post loop. It should look like this:
<?php query_posts("post_type=reviews"); ?>
<?php if (have_posts()) :?>
<?php while (have_posts()) : the_post(); ?>
<div class="post" >
<h2><a href="<?php the_permalink() ?>" ><?php the_title(); ?></a></h2>
<?php the_content(); ?>
</div><!-- Post ends -->
<?php endwhile; ?>
<?php else: ?>
<p>Sorry, we could not find what you were looking for</p>
<?php endif; wp_reset_query(); ?>
You need help getting .htaccess to convert your categories into hard URLs. I thought WP did this automatically so you'll want to look and make sure you have set up the directory permissions on WP so it can write to your .htaccess file.
Please read this guide and it will be cleared up.
Duplicate the file in your theme called "single.php" and rename it to "single-reviews.php"
Since "single.php" is used for your regular posts, you can append the name of the custom post type to the end of "single-" to automatically use that.
Now once inside of the file "single-reviews.php" you can adjust the layout to be how ever you want.
If you get a 404 error, or it is not showing you the correct layout, you may need to flush the rewrite rules. You can do this two ways.
Go to the permalinks page in your backend and it will sometimes auto flush them.
The best way to do it is in your "functions.php" file in your theme directory add the following code:
add_action ( 'init', 'flush_rewrite_rules' );
function flush_rewrite_rules()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
Create a new page called reviews. Create a new page template that calls the custom post type. Assign the page template to the page...
This is what i want to do:
I want /summary.php to include 5 latest posts (only the extract) from my blog, which lives in /wp.
Is there any way to include Wordpress in /summary.php and only print the html for these posts? (Maybe i should parse the rss?)
Take a look to Integrating WordPress with your Website
This is an example from that page, that shows the first ten posts in alphabetical order:
<?php
require('/the/path/to/your/wp-blog-header.php');
?>
<?php
$posts = get_posts('numberposts=10&order=ASC&orderby=post_title');
foreach ($posts as $post) : start_wp(); ?>
<?php the_date(); echo "<br />"; ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php
endforeach;
?>
Use $posts = get_posts('numberposts=10'); if you want the 10 latest posts.
Probably the easiest and most elegant way to do this is to create a custom theme to live on summary.php. The WP library exposes a number of functions for easy output of articles.
I think you have answered your self their. The RSS feed will give you the content of your latest posts.
With not much work you can just pull out the data you need
You can create a "clean" template, which you can apply to 'summary' page (this page must be a wordpress page too).
You can find an example here:
http://www.tyssendesign.com.au/articles/cms/fetching-posts-in-wordpress-expressionengine-with-jquery-ajax/
you can include wp-config.php, which will pull in the rest of the API. then you will be able to use wp functions like
function get_post($postID)
I am trying to customise a Wordpress theme. I have a function in themes/functions.php that I would like to run on some pages.
I need to be to:
Detect the page ID to determine whether the function should execute
Determine which hook to attach the function to (preferably something like page load.
Cheers
The file functions.php is for theme specific functions called inside your theme. If this is a theme specific function then the function call should be in the header (or wherever you want the output of the function to appear) via <?php my_function() ?>.
Hooks are for plugins, not template specific code.
If you are inside The Loop, then you can call <?php the_ID(); ?> as WarrenB said. If you are outside of the loop, then <?php echo $post->ID?> will print the page ID.
To the questions you posed, given you want to select on id #9, run your loop like this:
<?php
query_posts('page_id=9');
if (have_posts()) : while (have_posts()) : the_post();
// Do whatever on post id #9
?>
<?php endwhile; else: ?>
// Do whatever on all the other posts
<?php endif; ?>
If this isn't the answer you're looking for, please add more information to your question.
I use codeigniter and need to display last 3 posts at footer from blog as blabla.com/blog located.
when I create a test.php file as below. it works well,
test.php
<?php
// Include Wordpress
define('WP_USE_THEMES', false);
require('./blog/wp-blog-header.php');
query_posts('showposts=3');
?>
<ul>
<?php while (have_posts()): the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
but when I copy same code to footer_view.php of codeigniter structre, it doesnt work and giving error as below:
error at codeigniter footer_view:
Fatal error: Call to undefined method
stdClass::set_prefix() in
/blabla/blog/wp-settings.php
on line 268
any idea what can be the problem? :/ appreciate helps!!
I've used 3 tricks for getting WordPress content into CodeIgniter:
Pull via XMLHttpRequest from a custom WP template (skip headers/footers/sidebars). I like this method as it is highly decoupled, and it makes for fast page loads.
Pull via CURL or get_file*. This is similar to using XMLHttpRequests, but server side.
Wrap WP in a library. This is more work, but the essence is calling the core WP object from a CI library. I prototyped this method last year, but found that #1 performed better (and it allowed me to move the content to another server later).
Note, you could also IFrame the page, but IFrames seem a bit hackish given #1 and #2.
Have you thought about using the RSS feed from wordpress to display the blog posts with codeigniter? It would be a more flexible solution.