Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

How to put some custom PHP code at the top of every page in Vanilla

huntyhunty New
edited October 2014 in Vanilla 2.0 - 2.8

Hi there

I'm trying to execute some custom PHP code at the top of every page in Vanilla (using 2.1.3), said PHP code needing to be inserted before the html head.

I've found the file vanilla-core-2-1-3\vanilla\themes\bittersweet\views\default.master.tpl in the Bittersweet theme (which I am using) and added the PHP code as the first few lines inside that, but while this then code appears in all the pages when I do a view source, it doesn't actually appear to execute.

Is there a more fundamental/core design file I need to edit?

There used to be a file at applications\dashboard\'default.master.php' in version 2.0.18.8 which looks like it'd do the trick, but I can't find anything similiar to that in 2.1.3.

I've also tried using the 'Pockets' plugin to see if that will do the job, but its settings page blanks out on me, so no go-there . . . I presume the plugin is flaky with Vanilla 2.1.3.

Any ideas of how to slap some custom PHP code into every Vanilla page that will actually execute when run?

Ta

Comments

  • hgtonighthgtonight ∞ · New Moderator
    edited October 2014

    Welcome to the community!

    1. Create a simple plugin.
    2. Use the Base Render Before hook.
    3. Profit.

    The following code, placed in /plugins/ExecuteBeforeRender/class.executebeforerender.plugin.php should do the trick:

    $PluginInfo['ExecuteBeforeRender'] = array(
      'Name' => 'Execute Before Render',
      'Description' => 'Executes some arbitrary PHP code before render of every page.',
      'Version' => '0.1',
      'MobileFriendly' => TRUE,
      'Author' => 'hgtonight',
      'AuthorUrl' => 'http://www.daklutz.com',
      'License' => 'GPLv3'
    );
    
    class ExecuteBeforeRender extends Gdn_Plugin {
      public function Base_Render_Before($Sender) {
        // Put your code here
        echo 'sup';
      }
    }
    

    That said, there is possibly a better, more specific way to do this if you mind sharing why you want to run PHP on every page.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • BleistivtBleistivt Moderator
    edited October 2014

    Don't edit core files. You can put a bootstrap.before.php in you /conf/ directory, which will be excecuted on every pageload before vanilla bootstraps itself.

    We could probably help you better, if you describe what you are trying to do, maybe a simple custom plugin which handles Base_Render_Before would also be sufficient.

    Edit: @hgtonight beat me again :D

    As for the pockets plugin, try the one in the github repository, which is more up to date:
    https://github.com/vanilla/addons/tree/master/plugins/Pockets

  • R_JR_J Ex-Fanboy Munich Admin
  • A fourth possibility

    add method to themehooks file

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Thanks everyone for you suggestions - I'll give them a try and see if they work, starting with hgtonight's. I'm trying to call a password script so I can lock sight of the forum away from everyone except the forum's registered users.

    There are some settings in the dashboard that suggested Vanilla could do this by itself, but when I tried them, I can still view the forum posts without first logging in.

  • R_JR_J Ex-Fanboy Munich Admin

    You're right, that's a standard feature and you will not need to code anything for that. Just go to /dashboard/role and edit the Guests role. Uncheck the "View" permission by "Default Category Permissions" and eventually the "View" permission for categories with custom permissions. Don't forget to hit the Save button and that's it!

    1. Please reassure that the settings have been saved by reloading that page. Some users report problems when saving settings.
    2. Do you have upgraded/ported this installation? Some users report permission problems after a upgrade/port process.
  • @hunty said:
    Thanks everyone for you suggestions - I'll give them a try and see if they work, starting with hgtonight's. I'm trying to call a password script so I can lock sight of the forum away from everyone except the forum's registered users.

    If you are doing it this way you are probably not doing it right. passward authentication should be one time. Session authentication is different. You should never carry passwords around willy nilly. You make yourself vulnerable.

    Like R_J said in you case you need to look at roles and permissions.

    grep is your friend.

  • I tried R_J's route again, "Uncheck the "View" permission by "Default Category Permissions" and eventually the "View" permission for categories with custom permissions" and it worked this time, so job done.

    Thanks for all your help.

Sign In or Register to comment.