Read WordPress plugin settings from database

The final step to create your first WordPress plugin is to take the info that we registered with the WordPress database, and pull that into our plugin to change how it affects the site.

Change static settings to be database driven

When we decided to create the main WordPress plugin function, we told WordPress what our extra post info should be. We hard-coded EXTRA INFO, and to change it we’d have to alter the plugin script manually each time.

if( !function_exists("extra_post_info") ) {    function extra_post_info($content)   {     $extra_info = "EXTRA INFO";     return $content . $extra_info;   }  add_filter( 'the_content', 'extra_post_info' );  }

Use WordPress get_option function to pull info from database

To allow our plugin users to change the info from the dashboard, and then have that info reflected across all our posts, we want to use the get_option function to pull in the extra_post_info data we stored.

if( !function_exists("extra_post_info") ) {    function extra_post_info($content)   {     $extra_info = get_option( 'extra_post_info' );     return $content . $extra_info;   }  add_filter( 'the_content', 'extra_post_info' );  }

Completed plugin

Now when a plugin user types in NEW INFO into our plugin page, and clicks Save. The info is stored in the WordPress database, and then that new info is pulled into our posts.

Check out the full completed Extra Post Info WordPress plugin for the entire script built in this guide.

extra post info admin page read setting from database

Thoughts on “Read WordPress plugin settings from database

Leave a Reply