skip to Main Content

I need to add some of my own php files to an existing WordPress site (from Bitnami) so it can be accessed from a 3rd-party service. I don’t need to access anything from WordPress in my code, but I need my code to be accessible from a url for 3rd-party services.

Like this:

https://myWordPressSite.com. <- normal WP site

https://myWordPressSite.com/myCustomDirectory/generateSerial.php <- my code
https://myWordPressSite.com/myCustomDirectory/doSomething1.php <- my code
https://myWordPressSite.com/myCustomDirectory/doSomething2.php <- my code

How can I add my directory of code to the WordPress file structure, and how to access it from a URL?

3

Answers


  1. Chosen as BEST ANSWER

    It turns out it was much simpler than I thought, or any of the other answers presented here.

    The answer is to just add a directory of my own code inside the Wordpress home folder.

    In the case of a AWS/Lightsail/Bitnami/Wordpress package, the home directory is:

    /opt/bitnami/wordpress
    

    So I created a directory and put my code in it..

    /opt/bitnami/wordpress/myDirectory
    /opt/bitnami/wordpress/myDirectory/generateSerial.php
    /opt/bitnami/wordpress/myDirectory/doSomething.php
    

    And it was then accessible from a url like:

    https://myWordPressSite
    https://myWordPressSite/generateSerial.php //etc
    

    One small catch is that some Wordpress packages and some other Bitnami packages are laid out a bit different, so check on your specific installation.


  2. You can add a $_GET OR $_POST to the header as a plugin for WordPress.

    You will need a header of the plugin

    /*
    Plugin Name: name
    Plugin URI: http://www.com/
    Description: test
    Version: 1.0
    Author: your name
    Author URI: http://www.com/
    */
    

    The first part of the code would be. The code you put in here would show in the header when each page is call

    function test_head() {
        echo $_GET['name it some thing']; 
        echo $_POST['name it some thing'];          
    }
    

    To call the function you need to add this line of code

    add_action( 'wp_head', 'test_head' );
    

    To learn more about add_action you can read it at
    https://developer.wordpress.org/reference/functions/add_action/

    After you a Activate the plugin
    You can now call your web site with GET or POST base on what you added to the header.

    By doing it was a plugin you can use the WordPress backbone function

    Good Luck
    Karl K

    Login or Signup to reply.
  3. Create a folder in your theme’s root folder called myCustomDirectory and place your php file inside that.

    then add to your functions.php the following:

    This will handle the Rewrite Rules:

    add_action('init', 'custom_add_rewrite_rule');
    function custom_add_rewrite_rule() {
        add_rewrite_rule('^myCustomDirectory/generateSerial.php', 'index.php?generateSerial=true', 'top');
    }
    

    This will handle the WordPress Query Vars:

    function add_query_vars_filter( $vars ){
        $vars[] = 'generateSerial';
        return $vars;
    }
    add_filter( 'query_vars', 'add_query_vars_filter' );
    

    This will tell wordpress to pull the correct file:

    function load_custom_template($template){
        if(get_query_var('generateSerial') ){
             $template = get_template_directory() ."/myCustomDirectory/generateSerial.php";
        }
    
        return $template;  
    }
    
    add_filter('template_include', 'load_custom_template');
    

    make sure to flush your rewrite rules when you are done by going to: Settings > Permalinks and clicking ‘save changes’

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search