Today I Learned - Rocky Kev

TIL How to Summon WordPress in random PHP files

POSTED ON:

TAGS:

Say you had a file that was outside of WordPress.
For example, when you submit a form, you can compile the data with pure PHP, and not use any WordPress.

But maybe you want to call a wp_options setting, like a API key that you saved.

// THIS WON'T WORK
$sekrit_key = get_options('my_secret_api_key')

Well, then you NEED to get all the helper functions of WordPress.

You can do that with this snippet:

define( 'WP_USE_THEMES', false ); // Don't load theme support functionality
require( './wp-load.php' );

wp-load.php is the access to all functions of WordPress, that's all. The first line tells WordPress to load not the Theme files; maybe the files are necessary for your requirements, then remove the line.

Now, this works. get_options('my_secret_api_key')

REF:
https://wordpress.stackexchange.com/a/47052/132722


Related TILs

Tagged:

TIL Static Blocks vs Dynamic Blocks

A static block is a piece of content whose markup is known when the page is saved. The block saves its content and markup directly in the post content. A dynamic block is a piece of content whose markup and exact content are not known when the page is saved.

TIL how to convert a shortcode to a WP block

Traditionally, shortcodes were a way that plugin developers could provide users the ability to add specific plugin functionality anwhere on their site. But shortcodes are not very user friendly, nor was hunting down the relevant data you needed to render the correct data for the shortcode. Converting existing shortcodes to blocks provides a much greater user experience in all aspects.

TIL how WordPress does serverside

This isn't fully accurate, but for the means of describing server-side rendering, it's a good start!