I am trying to connect to Wordpress using the WPDB because it's such a beautiful class and also there are configurations that specified in wp-config.php so i won't need to specify it again.
I going to write a small separated script from main wordpress to run in background that will need to use this WPDB instance.
How can I archive this?
Any help is appreciated.
The best(fastest and safest) way to load only load the core functionality of WordPress is to use the SHORTINIT
flag like this:
define( 'SHORTINIT', true );
require( '/path/to/wp-load.php' );
//Here you can use WordPress core features, for example the $WPDB object
For more information about this and see what is loaded, is to check the code in /wp-settings.php
. There you will find the following section:
// Stop most of WordPress from being loaded if we just want the basics.
if ( SHORTINIT )
return false;
This means that anything after this won't be loaded, and it's quite a lot of things as you can see. The footprint will be much smaller than just loading the wp-load.php
and still gives you access to all the all the built in functions in WordPress core, unlike including for example /wp-includes/wp-db.php
directly. Many functions in WP core also has dependencies in other files and it can be a mess to figure out exactly what files you need to include to be able do what you want. SHORTINIT
includes the needed dependencies so you don't have to worry about this.
If you know exactly what you need, for example only WPDB, the fastest way is of course to only include the files you need, but SHORTINIT
provides a safer and more standardised way to load the WP core and the dependencies. With SHORTINIT
WordPress does not load plugins, most parts of the plugin API, themes, theme functions and most admin and frontend functions. This is where the heavy code is in a typical WordPress install. In most cases I think SHORTINIT
is worth the small tradeoff in speed/performance compared to including only the files you need and it's in most cases a huge performance boost compared to a full load.
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
include_once $path . '/wp-config.php';
include_once $path . '/wp-load.php';
include_once $path . '/wp-includes/wp-db.php';
include_once $path . '/wp-includes/pluggable.php';
// $wpdb is available, do stuff
'WEB > WP(WordPress)' 카테고리의 다른 글
Code To Integrate WordPress Media Uploader In Plugin or Theme (0) | 2018.04.10 |
---|---|
How to insert data using wpdb (0) | 2018.04.09 |
Get user role by ID Wordpress (0) | 2018.04.08 |
[function] get_post (0) | 2018.04.07 |
What action should I hook into when adding roles and capabilities? (0) | 2018.04.06 |
For reference, WP_Post OBJECT contains following fields:
WP_Post Object
(
[ID] =>
[post_author] =>
[post_date] =>
[post_date_gmt] =>
[post_content] =>
[post_title] =>
[post_excerpt] =>
[post_status] =>
[comment_status] =>
[ping_status] =>
[post_password] =>
[post_name] =>
[to_ping] =>
[pinged] =>
[post_modified] =>
[post_modified_gmt] =>
[post_content_filtered] =>
[post_parent] =>
[guid] =>
[menu_order] =>
[post_type] =>
[post_mime_type] =>
[comment_count] =>
[filter] =>
)
Expand full source code
Wouldn’t it be better practice to use get_the_title(..) in this case? directly accessing the post object’s data member would bypass applying filters and enforcing protected and private settings, unless that’s explicitly desired.
To get the title for a post with ID 7:
$post_7
= get_post( 7 );
$title
=
$post_7
->post_title;
Alternatively, specify the $output parameter:
$post_7
= get_post( 7, ARRAY_A );
$title
=
$post_7
[
'post_title'
];
If you need special things—[shortcodes], paragraph tags, anything exciting—in the content, you should apply the filters as opposed to using do_shortcode().
$post
= get_post( 42 );
$output
= apply_filters(
'the_content'
,
$post
->post_content );
For Reference : WP_Post Object has following properties, which are returned by get_post().
Member Variable Variable Type Notes
ID int The ID of the post
post_author string The post author's user ID (numeric string)
post_name string The post's slug
post_type string See Post Types
post_title string The title of the post
post_date string Format: 0000-00-00 00:00:00
post_date_gmt string Format: 0000-00-00 00:00:00
post_content string The full content of the post
post_excerpt string User-defined post excerpt
post_status string See get_post_status
for
values
comment_status string Returns: { open, closed }
ping_status string Returns: { open, closed }
post_password string Returns
empty
string
if
no password
post_parent int Parent Post ID (
default
0)
post_modified string Format: 0000-00-00 00:00:00
post_modified_gmt string Format: 0000-00-00 00:00:00
comment_count string Number of comments on post (numeric string)
menu_order string Order value
as
set through page-attribute when enabled (numeric string. Defaults to 0)
Expand full source code
If you have a shortcode in the content you should use:
$post
= get_post( 4304,ARRAY_A );
$output
= do_shortcode(
$post
[
'post_content'
]);
If you want to get a post by slug, you could do a new WP_Query, or use the get_page_by_path function: https://developer.wordpress.org/reference/functions/get_page_by_path/
You’ll need to use the post_type if you are not getting a page.
To Get author of the Post
$post_info
= get_post( 10 );
$author
=
$post_info
->post_author;