• Resolved zanagb

    (@zanagb)


    Hello. I am not exactly sure if this is the place to write my question, but i’m in the midst of doing a website rebuild, with admittedly limited knowledge, and lots of peculiar requirements.

    As of now, we have a rather complex system where our main theme integrates a lot of functions and handles a lot of the operation. The codebase is unmaintainable, it was last updated many years ago, and breaks with PHP 8, which means it is time to start from scratch.

    One of the functions the theme seems to handle, is a script that generates watermarks on-the-fly as soon as any given user loads the page or relevant image. The file itself seems to handle everything once given a certain URL. This means that if the user is logged in, and gets the full URL to an image, a version is made specifically for them. This version seems to also be created if they just access the post containing the image. Attempts at reverse engineering the theme to search for calls to the specific file have been futile as there are no include or require clauses explicitly calling the file. So with that ouf of the question, i feel a different approach is needed. Namely, salvage the file that contains the logic and find a way to strap it somewhere anytime an user requests a post or image URL.

    Does WordPress provide a way for a plugin to run a file at all times for non-admin users that visit any post/image of the website? Alternatively, what is the most appropiate way to call a script on page load for such users? The file containing the main logic seems standalone, so it’s a matter of calling it so it can get the parameters it requests for ( or latch into WP to fetch the contents directly without them being passed to it )

    • This topic was modified 1 year, 10 months ago by zanagb. Reason: fixed typo
Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi @zanagb

    Does WordPress provide a way for a plugin to run a file at all times for non-admin users that visit any post/image of the website?

    what is the most appropiate way to call a script on page load for such users?

    WordPress provides a hooks system that allows you to run PHP code at various points of the page load:

    add_action( 'init', 'my_function' );
    function my_function() {
        echo 'hello';
    }

    Note that above I’m using init as an example, you should take a look at the list of actions to find the one that is most appropriate to your use case. It could also be that you’d want to use a filter instead of an action, you can find a list of filters here.

    Moderator bcworkz

    (@bcworkz)

    WordPress can do all manner of customized things when someone requests one of its pages through hooks like Paulo has described. It can also change the appearance of images on its pages. However, When an image file is directly requested, even when it’s in /wp-content/uploads/, WordPress is not involved in the process at all. A completely separate app would need to be involved to dynamically process direct image requests.

    Or you could block direct image access through .htaccess directives and only allow the viewing of images within the WordPress context where dynamic processing would be feasible.

    Thread Starter zanagb

    (@zanagb)

    Well, i want to thank you two for the responses. I’ll definitely be looking into removing direct file access through an .htaccess file. It’ll make our lives a lot easier, management wise, so we can just focus on registered users looking at content through the site.

    As for using hooks for the file. This absolutely shows my lack of knowledge, but, i already have a plugin i’ve been in the process of applying fixes to.

    Since it does not have any functions involved on the non-admin usage of the site, could i use the existing plugin, say, in the main file through an include/require to keep the script loaded and execute it with the init hook? ( which seems to be the earliest point on which users are authenticated ), or would i need to turn the script, which only contains the manipulation logic, into it’s own plugin? I understand it might be a silly question, but it would be great to keep all the bespoke things bundled together if the hooks don’t cause any conflict.

    Plugins can run arbitrary code, so you could invoke the existing script from a new plugin you create, or you could adapt an existing plugin to call the script.

    Let’s assume you created a new plugin. In that plugin’s plugin.php you can add a function that invokes the script, then call that function using the hooks system, as I mentioned above:

    function run_script() {
       // Here you would invoke the script
    }
    
    // (Editor formats this wrongly, should be a single line)
    add_action( 'init', 'call_script' );

    The way you invoke the script in the run_script() function will depend on how the script itself is programmed. If it exposes a function you can call, you would just call said function from your run_script() function:

    function run_script() {
        require_once( 'my_script.php' );
        function_from_script();
    }

    If the script does not expose a function but instead runs as an inline script, importing its code will automatically run it:

    function run_script() {
        require_once( 'my_script.php' );
    }

    I hope this helps.

    • This reply was modified 1 year, 10 months ago by Paulo Pinto.
    • This reply was modified 1 year, 10 months ago by Paulo Pinto.
    Thread Starter zanagb

    (@zanagb)

    Hello again @psrpinto . Yes, that does help, that’s very concise and does explain a lot of what i need to get that working. Thank you. That’s very much appreciated.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Run script on page/image load?’ is closed to new replies.