Search code examples
wordpresscustom-wordpress-pages

how to create public file in wp plugin to access it from other apps?


I want to write a plugin so that I can connect to it with an app bot, and store information inside the database.

Note that because I directly connect to php file inside this plugin, the public WordPress values like $wpdb are not known.

And how can i access to wp functions? such as get_option,...

thanks for your help.


Solution

  • Maybe this helps you

    require_once( 'path to wordpress project root'. '/wp-load.php' );
    
        // Set up the WordPress query.
    wp();
    

    For example if you want to create plugin with name - test

    1. You need to create a directory with name 'test' inside plugin directory wp-content/plugins/test
    2. Then you need to create file test.php - it is the main file of your plugin
    3. Then you need to add at beginning of your test.php file after comments require wp-load if WordPress environment is not included (it is your case when you call your file directly) For example, your plugin main file is wp-content/plugins/test/test.php starts with this snippet and before this snippet, you need to place your plugin metadata like name - description and so on. I recommend to download WordPress and see default plugins meta in wp-content/plugins/askimet/askimet.php and wp-content/plugins/hello.php.

      if ( !defined('ABSPATH') ) { /** Set up WordPress environment */ require_once( '../../wp-load.php' ); } then you can use get_option() $wpdb and other wp functions.

      wp() function in my previous answer need for initializing global wp query. It is not necessary. You can create your own wp query by using WP_Query class. https://codex.wordpress.org/Class_Reference/WP_Query - there is description with examples how to use WP_Query.

    4. Then You can write your plugin in OOP or functional style. You can write your plugin analogically to Askimet for example or with boilerplates. This is the question about plugin development. Hope this question helps you achieve your purpose. https://wordpress.stackexchange.com/questions/85486/is-there-any-plugin-development-framework

    Hope this help you