[3.2][BETA] Custom Code

A place for Extension Authors to post and receive feedback on Extensions still in development. No Extensions within this forum should be used within a live environment!
Suggested Hosts
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

IMPORTANT: Extensions Development rules

IMPORTANT FOR NEEDED EVENTS!!!
If you need an event for your extension please read this for the steps to follow to request the event(s)
Post Reply
User avatar
martti
Registered User
Posts: 682
Joined: Thu Jul 31, 2014 8:23 am
Location: Belgium

[3.2][BETA] Custom Code

Post by martti » Sat Nov 15, 2014 2:35 pm

Extension Name: Custom Code
Author: martti

Extension Description: Allows to add custom template code on template event locations. See the second post for inspiration on how to use. This extension can only be used when inclusion of PHP with the PHP and INCLUDEPHP template tags is disabled on your board (for security).

Extension Version: 2.0.1
Requirements:
Extension Download: https://github.com/marttiphpbb/phpbb-ex ... master.zip
The files are to be put in ext/marttiphpbb/customcode

Github repository: https://github.com/marttiphpbb/phpbb-ext-customcode

Languages: ar, en, es, nl, it, tr (non-en languages need to be updated for a few keys)

Templates: all

Screenshots: (these need to be updated)
Image
Image
Image
Last edited by martti on Mon Nov 05, 2018 8:14 am, edited 49 times in total.

User avatar
martti
Registered User
Posts: 682
Joined: Thu Jul 31, 2014 8:23 am
Location: Belgium

Re: [ALPHA] Custom Code

Post by martti » Sat Nov 15, 2014 2:35 pm

Use case: Piwik analytics.

You want to install Piwik analytics on your board.
Of course you can use this Custom Code extension to include tracking code from any other analytics software. But Piwik is my favourite, because with Piwik you don't hand your users' data over to a third party; you run the analytics software on your own server and so you stay master over your own data.

After installing Piwik (follow the instructions from their site), you can directly insert the tracking code in overall_footer_after.html (copy - paste)
ACP > Extensions > Custom Code > Edit :: overall_footer_after.html

Or you could create a separate file where you copy-paste the Piwik tracking code.
ACP > Extensions > Custom Code > Files > Create file :: piwik.html
ACP > Extensions > Custom Code > Edit :: piwik.html
Copy-paste the Piwik tracking code.
Then include piwik.html in overall_footer_after.hml
ACP > Extensions > Custom Code > Edit

Code: Select all

{%- include CUSTOMCODE_PATH ~ 'piwik.html' -%}
Note: before version 2.0.0 this was
<!-- INCLUDE ../../../../../../store/customcode/piwik.html -->
You could also create a link in the footer for easy access to the analytics data visible for admins only:
(Supposing you installed Piwik in the /piwik subdirectory from your web root.)
ACP > Extensions > Custom Code > Edit :: overall_footer_copyright_after.html

Code: Select all

{%- if U_ACP -%}<a href="./piwik">Piwik</a>{%- endif -%}

Use case: Styling.

You want a picture with colored baby chickens in the background of the headerbar of prosilver.

Insert some inline styling:
ACP > Extensions > Custom Code > Edit :: overall_header_stylesheets_after.html

Code: Select all

{#- Baby chickens in headerbar styling -#}
<style>
.headerbar {
	background-image: url("{{- ROOT_PATH -}}images/baby_chickens.jpg");
}

{# remove the logo and increase the height of the headerbar #}
.imageset.site_logo {
    background-image: none;
    padding-left: 0;
    padding-top: 100px;
}
</style>
Note: {# and #} are Twig comment tags. Any text inside will not be included in the final template.
Take a picture of your colored baby chickens and crop it to a little bit more than 1100 px x 100 px and upload it as baby_chickens.jpg to the directory /images.

Image

You could also make the image in the headerbar dependent on the forum which you are in:

First select and crop your images (1100x100). Save them in the /images folder.

Add inline styling (select according to the forum ids of your forum).
Edit :: overall_header_stylesheets_after.html

Code: Select all

{#- headerbar styling depending on forum -#}
<style>
.headerbar {
    {%- if  FORUM_ID == 2 -%}
        background-image: url("{{- ROOT_PATH -}}images/bananas.jpg");
    {%- elseif  FORUM_ID == 9 -%}
        background-image: url("{{- ROOT_PATH -}}images/apples.jpg");
    {%-  else -%}
        background-image: url("{{- ROOT_PATH -}}images/kiwis.jpg");  /* default image */
    {%- endif -%}
}

{#- remove the logo and increase the height of the headerbar -#}
.imageset.site_logo {
    background-image: none;
    padding-left: 0;
    padding-top: 80px;
}
</style>
Image


Image


Image

See here for a random header example:
viewtopic.php?f=456&t=2275361&start=165#p13910806

Tip: enable caching with header Cache-Control: "max-age=31536000, public" (60*60*24*365 = 31536000 seconds in one year)
If you use Apache server, you can set this in the httpd.conf file:

Code: Select all

<Directory "/some/dir/images"">
   ExpiresActive On
   ExpiresDefault "access plus 1 year"
   Header append Cache-Control "public"
</Directory>
Or in .htaccess

Code: Select all

Header set Cache-Control "max-age=31536000, public"
Use case: Welcome message.

You want to show a welcome message on the Index to Anonymous guests only (everyone that's not logged in):

ACP > Extensions > Custom Code > Edit :: overall_header_content_before.html

Code: Select all

{#- Welcome message -#}
{%- if not S_USER_LOGGED_IN and SCRIPT_NAME == 'index' -%}
<div class="post welcome">
	<h2>Welcome to our forum</h2>
	<p>Please take some time to register yourself to have full access.</p>
</div>
{%- endif -%}
Styling
ACP > Extensions > Custom Code > Edit :: overall_header_stylesheets_after.html

Code: Select all

{# Welcome styling #}
<style>
div.welcome
{
   border: 1px solid #444466;
}
</style>
Image

Template Variables
As you can see in the examples, you can use all the available template variables.
The extension produces also template variables on its own: the query parameters
of the current url are available and can be used like this:

Code: Select all

{%- if SCRIPT_NAME == 'ucp' and marttiphpbb_customcode.query.i == 'pm'  -%}
Your content for the private messages pages only  ...
{%- endif -%}
Note: before version 2.0.x the format was
CUSTOMCODE_PARAM_I == 'pm'
Here a short list of some of the most useful core template variables:
  • SCRIPT_NAME gives the script: index, viewtopic, viewforum, ucp, mcp, faq, etc.
  • S_USER_LANG makes it possible to render different content depending on the language, example value: en-gb
  • .S_FIRST_ROW, .S_LAST_ROW, .S_ROW_COUNT are iteration selectors to select certain rows. For example, if you want to select the first post on the page in viewtopic <!-- IF postrow.S_FIRST_ROW --> your content <!-- ENDIF --> (see Google Adsense example below)
  • S_USER_LOGGED_IN wether the user is logged in.
  • U_ACP link to the acp. Useful to check if the user is an administrator.
  • U_MCP link to mcp. Useful to check if the user has moderation rights.
  • T_TEMPLATE_NAME. The current template i.e. prosilver
  • FORUM_ID The current forum id (not set when not in forum context.)
  • FORUM_NAME The current forum name (not set when not in forum context.)
  • ROOT_PATH The web root path of your board.
If you wish to check with template variables which groups the user is in, have a look at this extension.

Use case: Google Adsense

You want to put Google Adsense advertisements in topics.

(For more control and launching your own ad campaigns - or mixing with Google adsense you could also consider running your own ad server with Revive)

To insert the Google adsense code after the first post:
Edit :: viewtopic_body_postrow_post_after.html

Code: Select all

{#- Google adsense -#}
{%- if postrow.S_FIRST_ROW -%}

Your Google adsense code here...

{%- endif -%}
If you wish to insert adverts in multiple places (for example after the first, the 5th and the 10th post):

Edit :: viewtopic_body_postrow_post_after.html

Code: Select all

{# Google adsense -#}
{%- if postrow.S_FIRST_ROW or postrow.S_ROW_COUNT == 5 or postrow.S_ROW_COUNT == 10  -%}

Your Google adsense code here...

{%- endif -%}
Image

Add some styling:

Edit :: viewtopic_body_postrow_post_after.html

Code: Select all

{#- Google adsense -#}
{%- if postrow.S_FIRST_ROW or postrow.S_ROW_COUNT == 5 or postrow.S_ROW_COUNT == 10  -%}
<div class="post adsense">
Your Google adsense code here...
</div>
{%- endif -%}
Edit :: overall_header_stylesheets_after.html

Code: Select all

{#-  adsense styling -#}
<style>
div.adsense
{
   background-color: #ddeedd;
}
</style>
Image

To insert Google Adsense code in between the categories on the index page:

Edit :: forumlist_body_category_header_before.html

Code: Select all

{#- Google Adsense -#}
{%- if forumrow.S_IS_CAT and not forumrow.S_FIRST_ROW -%}
   <div class="post adsense">
      Your Google adsense code here...
   </div>
{%- endif -%}
Image

More hints on Google Adsense by GoBieN: viewtopic.php?f=456&t=2275361&start=180#p13913446


Use case: Message on the contact page

Edit :: overall_header_content_before.html

Code: Select all

{#- Contact message -#}
{%- if SCRIPT_NAME == 'memberlist' and marttiphpbb_customcode.query.mode == 'contactadmin' -%}
<div class="post contact">
<h2>Please,</h2>
<p>use this contact form only for urgent questions about the board and not for private help.</p>
</div>
{%- endif -%}
Styling
Edit :: overall_header_stylesheets_after.html

Code: Select all

{#- Contact styling -#}
<style>
div.contact
{
    background-color: #ffeeee;    
}
</style>
Image

Including other template events In order to keep the extension simple, only a limited set of template events is included. These should cover already a lot of flexibility and use cases. If you need to put some template code on a place where this extension has no template event available, then your options are:
  • Use javascript to move your code to this place. Put your code in a div element in the overall_footer_body_after template event and then move it in its location with Javascript. See an example here
  • Fork this extension and include the template event(s) you need. See here how to do this. Here is the repository on Github.
User Groups
To show content to users only of a certain group.
  • Install en enable the Group Template Variables extension.
  • Find out the id of the group. For example, go to 'Manage Groups' in the ACP and hover over the edit links of the groups. In the url you'll find the group id with parameter g for example: &g=7
  • Use the template variable S_GROUP_x in a conditional statement (replace x with the group id) like

    Code: Select all

    <!-- IF S_GROUP_7 -->
    Content only visible to members of the group with id 7 ...
    <!-- ENDIF -->
Last edited by martti on Sun Jun 10, 2018 9:26 pm, edited 51 times in total.

User avatar
Walther
Registered User
Posts: 268
Joined: Fri Jul 09, 2004 5:21 pm
Location: The Netherlands

Re: [ALPHA] Custom Code

Post by Walther » Sat Nov 15, 2014 2:47 pm

When chosen for Create or delete custom files, next error on top of screen.

Code: Select all

array(0) { } [phpBB Debug] PHP Warning: in file [ROOT]/includes/functions_acp.php on line 132: Cannot modify header information - headers already sent by (output started at [ROOT]/ext/marttiphpbb/customcode/acp/main_module.php:107)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions_acp.php on line 132: Cannot modify header information - headers already sent by (output started at [ROOT]/ext/marttiphpbb/customcode/acp/main_module.php:107)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions_acp.php on line 132: Cannot modify header information - headers already sent by (output started at [ROOT]/ext/marttiphpbb/customcode/acp/main_module.php:107)
Also lot of spaces in the form field ?

User avatar
martti
Registered User
Posts: 682
Joined: Thu Jul 31, 2014 8:23 am
Location: Belgium

Re: [ALPHA] Custom Code

Post by martti » Sat Nov 15, 2014 2:57 pm

Walther wrote:When chosen for Create or delete custom files, next error on top of screen.
It should be fixed now. I left a var_dump() by accident at /acp/main_module.php:107
Also lot of spaces in the form field ?
What do you mean by that?

User avatar
Walther
Registered User
Posts: 268
Joined: Fri Jul 09, 2004 5:21 pm
Location: The Netherlands

Re: [ALPHA] Custom Code

Post by Walther » Sat Nov 15, 2014 3:11 pm

The error is still there, after changing main_module.php line 107
Also purged the cache.

Also, the files made do not show, until you press one [CREATE] (even with no file name given to create)
An error comes (logical: no file name given, so is normal), then you press "Back to previous page" and then the contents of /store/customcode/ is being read and the premade files (as test: piwik.html) shows and can be selected to be deleted.

When choosen to read the file, a lot of empty spaces precede the content of the file.
edit: preceding tabs, 4 according to http://www.lettercount.com/

Also, can you give a sample syntax of the include ?
include("../../../../../../store/customcode/piwik.html"); clearly doesn't work :)
Last edited by Walther on Sat Nov 15, 2014 3:17 pm, edited 1 time in total.

User avatar
martti
Registered User
Posts: 682
Joined: Thu Jul 31, 2014 8:23 am
Location: Belgium

Re: [ALPHA] Custom Code

Post by martti » Sat Nov 15, 2014 3:17 pm

Walther wrote: Also, can you give a sample syntax of the include ?
include("../../../../../../store/customcode/piwik.html"); clearly doesn't work :)

Code: Select all

<!-- INCLUDE ../../../../../../store/customcode/piwik.html --> 

User avatar
martti
Registered User
Posts: 682
Joined: Thu Jul 31, 2014 8:23 am
Location: Belgium

Re: [ALPHA] Custom Code

Post by martti » Sat Nov 15, 2014 3:32 pm

Walther wrote: When choosen to read the file, a lot of empty spaces precede the content of the file.
edit: preceding tabs, 4 according to http://www.lettercount.com/
I don't know yet where the tabs come from.
Last edited by martti on Sun Jan 18, 2015 9:48 am, edited 1 time in total.

User avatar
martti
Registered User
Posts: 682
Joined: Thu Jul 31, 2014 8:23 am
Location: Belgium

Re: [ALPHA] Custom Code

Post by martti » Sat Nov 15, 2014 3:43 pm

Walther wrote:The error is still there, after changing main_module.php line 107
Also purged the cache.
Did you remove this:

Code: Select all

 var_dump($selected_files); 
Then the error should be gone. (Github is updated.)
Also, the files made do not show, until you press one [CREATE] (even with no file name given to create)
An error comes (logical: no file name given, so is normal), then you press "Back to previous page" and then the contents of /store/customcode/ is being read and the premade files (as test: piwik.html) shows and can be selected to be deleted.
Not sure if I understand you, but possibly this is just a matter of fine-tuning.

User avatar
Walther
Registered User
Posts: 268
Joined: Fri Jul 09, 2004 5:21 pm
Location: The Netherlands

Re: [ALPHA] Custom Code

Post by Walther » Sat Nov 15, 2014 4:39 pm

martti wrote:<!-- INCLUDE ../../../../../../store/customcode/piwik.html -->
:oops: ... html
Was thinking in php, also tried <?php include("../../../../../../store/customcode/piwik.html"); ?> :lol:

Yes, I did remove line 107: var_dump($selected_files);
It is fixed after I logged in again, so I should not only have purged the cache, but also the session I quess
martti wrote:Not sure if I understand you,
It's fixed.
When you go to the [create or delete custom files], the contents of /store/customcode is read and shown in the file index

The preceeding spaces are not in the actual file, only when you go to [edit custom file]
Opening the file with a file editor shows it is stored correctly
https://www.dropbox.com/s/9rdzho7jmohak ... e.png?dl=0

Here how it shows in notepad++
https://www.dropbox.com/s/x7loodfwoyq8y ... e.png?dl=0

Nothing really disturbing.

edit: (don't have Github (yet))
Dutch translation -- > phpbb-ext-customcode / language / nl / common.php

Code: Select all

<?php
/**
*  phpBB Extension - marttiphpbb customcode
* @copyright (c) 2014 marttiphpbb <info@martti.be>
* @license http://opensource.org/licenses/MIT
*/
if (!defined('IN_PHPBB'))
{
	exit;
}
if (empty($lang) || !is_array($lang))
{
	$lang = array();
}
// DEVELOPERS PLEASE NOTE
//
// All language files should use UTF-8 as their encoding and the files must not contain a BOM.
//
// Placeholders can now contain order information, e.g. instead of
// 'Page %s of %s' you can (and should) write 'Page %1$s of %2$s', this allows
// translators to re-order the output of data while ensuring it remains correct
//
// You do not need this where single placeholders are used, e.g. 'Message %d' is fine
// equally where a string contains only two placeholders which are used to wrap text
// in a url you again do not need to specify an order e.g., 'Click %sHERE%s' is fine
$lang = array_merge($lang, array(
 
	'ACP_CUSTOMCODE'							=> 'Aangepaste Code',
	'ACP_CUSTOMCODE_EDIT'						=> 'Bewerk aangepast code bestand',
	'ACP_CUSTOMCODE_EDIT_EXPLAIN'				=> 'Alle bestanden staan opgeslagen onder store/customcode. De originele template bestanden zijn niet gewijzigd. Om je zelf gemaakte bestand in te sluiten, gebruik INCLUDE, INCLUDECSS (in overall_header_head_append.html) of INCLUDEJS (bijv. in overall_footer_after.html). In de INCLUDE, INCLUDECSS of INCLUDEJS opdracht moet je je bestandsnaam vooraf laten gaan met ../../../../../../store/customcode/  <br/>Purge de cache om de wijzigingen te laten werken.',
	'ACP_CUSTOMCODE_CREATE_DELETE'				=> 'Maak of verwijder bestanden met aangepaste code',
	'ACP_CUSTOMCODE_CREATE_DELETE_EXPLAIN'		=> 'Deze bestanden staan onder store/customcode/',
	'ACP_CUSTOMCODE_CREATE'						=> 'Maak',
	'ACP_CUSTOMCODE_NEW_FILE'					=> 'Nieuw aangepaste code bestand',
	'ACP_CUSTOMCODE_DELETE'						=> 'Verwijder',
	'ACP_CUSTOMCODE_DELETE_EXPLAIN'				=> 'Bestanden direct verbonden met template gebeurtenissen (E) kunnen niet worden verwijderd. Gebruik de editor voor het wijzigen of verwijderen van code.',
	'ACP_CUSTOMCODE_DELETE_SELECTED'			=> 'Verwijder geselecteerd',
	'ACP_CUSTOMCODE_SELECT_ALL'					=> 'Selecteer alles',
	'ACP_CUSTOMCODE_UNSELECT_ALL'				=> 'De-selecteer alles',
	'ACP_CUSTOMCODE_FILES'						=> 'Bestanden',
	'ACP_CUSTOMCODE_FILE'						=> 'Bestand',
	'ACP_CUSTOMCODE_FILE_EXPLAIN'				=> 'Bestanden direct ingesloten met template gebeurtenissen zijn gemarkeerd met (E)',
	'ACP_CUSTOMCODE_EDITOR_ROWS'				=> 'Invoerveld regels',
	'ACP_CUSTOMCODE_SAVE'						=> 'Opslaan',
	'ACP_CUSTOMCODE_FILE_SAVED'					=> 'Het bestand is succesvol opgeslagen!',
	'ACP_CUSTOMCODE_FILE_NOT_EXIST'				=> 'Dit bestand bestaat niet.',
	'ACP_CUSTOMCODE_NOT_WRITABLE'				=> 'Bestand is niet beschrijfbaar.',
	'ACP_CUSTOMCODE_FILE_CREATED'				=> 'Bestand is gemaakt.',
	'ACP_CUSTOMCODE_FILENAME_EMPTY'				=> 'Bestandsnaam was niet ingevuld.',
	'ACP_CUSTOMCODE_FILE_NOT_CREATED'			=> 'Bestand kon niet worden gemaakt.',
	'ACP_CUSTOMCODE_FILE_ALREADY_EXISTS'		=> 'Bestand bestaat al.',
	'ACP_CUSTOMCODE_FILES_DELETED'				=> 'Het bestand is / de bestanden zijn verwijderd.',
	'ACP_CUSTOMCODE_NO_FILE_SELECTED'			=> 'Geen bestand geselecteerd.',
	'ACP_CUSTOMCODE_FILE_DOES_NOT_EXIST'		=> 'Bestand geselecteerd voor verwijderen is niet aangetroffen.',
	'ACP_CUSTOMCODE_FILE_NOT_DELETED'			=> 'Verwijderen van bestand MISLUKT.',
	
 
));

leschek
Registered User
Posts: 788
Joined: Tue Jul 18, 2006 12:49 pm
Contact:

Re: [ALPHA] Custom Code

Post by leschek » Sat Nov 15, 2014 5:57 pm

Nice. Finally I don't have to add code for visitor's counter into overall_footer of every style.

User avatar
Walther
Registered User
Posts: 268
Joined: Fri Jul 09, 2004 5:21 pm
Location: The Netherlands

Re: [ALPHA] Custom Code

Post by Walther » Sat Nov 15, 2014 6:06 pm

leschek wrote:Nice.
One could also delete the phpbb/googleanalytics extension and use this one.
As a matter of fact, when you want to use for example the 'Demographics' option of Google Analytics, the phpbb/googleanalytics does not provide that extra line of code ga('require', 'displayfeatures');

This is way better, just a google_analytics.html file in /store/customcode/ with the right code, and include it 8-)

User avatar
martti
Registered User
Posts: 682
Joined: Thu Jul 31, 2014 8:23 am
Location: Belgium

Re: [ALPHA] Custom Code

Post by martti » Mon Nov 17, 2014 12:23 pm

The tabs-in-editor-issue is fixed now.

The tabs were in the template /adm/style/edit.html

Before:

Code: Select all

	<textarea name="file_data" id="file_data" style="font-family:'Courier New', monospace;font-size:9pt;line-height:125%;width:100%;" cols="80" rows="{TEXT_ROWS}">
		{FILE_DATA}
	</textarea>
After:

Code: Select all

	<textarea name="file_data" id="file_data" style="font-family:'Courier New', monospace;font-size:9pt;line-height:125%;width:100%;" cols="80" rows="{EDITOR_ROWS}">{FILE_DATA}</textarea>
Last edited by martti on Mon Nov 17, 2014 1:19 pm, edited 2 times in total.

User avatar
martti
Registered User
Posts: 682
Joined: Thu Jul 31, 2014 8:23 am
Location: Belgium

Re: [ALPHA] Custom Code

Post by martti » Mon Nov 17, 2014 12:38 pm

Walther wrote: Dutch translation -- > phpbb-ext-customcode / language / nl / common.php
Thank you! I want improve the user interface, so there are still going to be quite some changes in the translation file.

User avatar
Walther
Registered User
Posts: 268
Joined: Fri Jul 09, 2004 5:21 pm
Location: The Netherlands

Re: [ALPHA] Custom Code

Post by Walther » Mon Nov 17, 2014 5:16 pm

martti wrote:The tabs were in the template /adm/style/edit.html
wow, sneaky
Only when you line those lines of code up, you can see it.
...width:100%;" cols="80" rows="{TEXT_ROWS}"> {FILE_DATA} </textarea>
...width:100%;" cols="80" rows="{EDITOR_ROWS}">{FILE_DATA}</textarea>

leschek
Registered User
Posts: 788
Joined: Tue Jul 18, 2006 12:49 pm
Contact:

Re: [ALPHA] Custom Code

Post by leschek » Tue Nov 18, 2014 8:44 pm

I would like to ask if you can add some kind of protection for edited files. When I updated "Custom code" (using "Upload extension" extension) it replaced edited files with new ones.

Post Reply

Return to “Extensions in Development”

Who is online

Users browsing this forum: </Solidjeuh>, andares, Mannix_, nou nou, strelok-by and 85 guests