Topic on Project:Support desk

Jump to navigation Jump to search

How to run custom PHP over MediaWiki

23
182.232.50.87 (talkcontribs)

I have created a PHP file which loads an HTML-frontend custom-developed "call now" position:absolute "sticky" button module.

I want to run that PHP file every time a MediaWiki page is loaded, so that this custom developed module would be loaded for each MediaWiki page and thus available to any user on any webpage in my website.


How can this be done?

Jonathan3 (talkcontribs)

If you really need to add PHP to each page you could edit the skin - /skins/Vector/includes/VectorTemplate.php maybe?

If you just need to add a button to each page maybe you could use MediaWiki:Sitenotice (see Manual:Interface/Sitenotice). If the button has HTML/Javascript that can't usually go on a wiki page you could try Extension:Widgets or similar.

182.232.50.87 (talkcontribs)

@Jonathan3

I have actually created two PHP modules, one is a contact form and one is a the "call now".

I can access the contact form by example.com/contact_form/contact_form.php but in contrast, example.com/call_now/call_now.php is of course something which is not a standalone webpage, rather just a caller for some HTML-CSS.


I can understand why you recommend to edit a skin's main PHP file; in theory I could paste all the PHP code currently in call_now.php inside the skin's main PHP file but the problem with that is that it each time I would upgrade MediaWiki I would have to re-paste it there again.


I think that what I need is to to execute my code from within MediaWiki GUI somehow (just as I paste JavaScript in Common.js and CSS in Common.css).

Jonathan3 (talkcontribs)

Could you not just add an include line to the skin?

Jonathan3 (talkcontribs)

What is the HTML/CSS in the "call now" button?

182.232.50.87 (talkcontribs)

@Jonathan3 sure I could but I aspire to untouch the skin's PHP (Maybe I should add such in include in LocalSettings.PHP?).

The HTML for basic functionality is:

main_box.php

<!DOCTYPE html>
<html>
	<head>
		<?php
			include './assets/assets.php';
		?>
	</head>
	<body>
		<div dir="rtl" class="cacb_main_box">
			<?php
				include './phone_box.php';
				include './email_box.php';
			?>
		</div>
	</body>
</html>

phone_box.php

<!DOCTYPE html>
<html>
	<head>
		<?php
			include './assets/assets.php';
		?>
	</head>
	<body>
        <div class="cacb_phone_box">
            <a class="cacb_phone_link" href="tel:URI">
                <img class="cacb_phone_icon" src="./images/VECTOR.svg"></img>
                <span class="cacb_phone_text">Call now</span>
            </a>
        </div>
	</body>
</html>
Bawolff (talkcontribs)
182.232.50.87 (talkcontribs)

@Jonathan3 sadly, uploading the directory of the module to my website application root (and then using include './MODULE'; in LocalSettings.php, didn't help.

182.232.50.87 (talkcontribs)

include './MODULE/main_box.php';

*

The " index.php " if you will, of the module.

Jonathan3 (talkcontribs)

Does anything work where you put that line, eg echo "hello"; ?

Maybe double check the path.

Jonathan3 (talkcontribs)

I've just looked on the computer and see your code. Maybe the "./" parts of the paths (for current directory) don't work when the file is included from a file in another directory, so should be changed.

Jonathan3 (talkcontribs)

"Maybe I should add such in include in LocalSettings.PHP?"

You could try it. If you "echo" some text in the middle of that file, it'll get printed at the very top of the page, completely separately from the MediaWiki page (if that makes sense). So maybe you could add your buttons there.

Or if you find a suitable hook you could maybe add some code to LocalSettings.php, sort of like a mini-extension without needing to write an extension.

182.232.50.87 (talkcontribs)

@Jonathan3 the code works for me in a web browser with the ./ parts

The full code is available here:

codereview.stackexchange.com/questions/268069/php-processed-html-css-cms-agnostic-contact-now-panel


A hook might be the way to go, but I feel quite lost in Manual:Hooks. Should I create a JSON command? What, ideally should such JSON command do?... What should be the condition for the reaction of executing my code?

182.232.50.87 (talkcontribs)

In JavaScript that condition might be:

window.addEventListener('load', () => {
});

But in PHP I don't know.

Jonathan3 (talkcontribs)

"the code works for me in a web browser with the ./ parts"

Does it work in your web browser when included from a php file in a different directory?

182.232.50.87 (talkcontribs)

Sadly, no.

Jonathan3 (talkcontribs)

All right then, maybe change the paths and see if that helps. Get it to work like this, then it might also work within the skin.

182.232.50.87 (talkcontribs)

The paths are in order; I prefer not to use it in the skin at all.

For possible future readers: In the end I would take the HTML-CSS only path (without using PHP format which helps me to well organize the code, in that case), as I have explained below.

Jonathan3 (talkcontribs)

There are lots of simple ways to achieve what you want. Unfortunately I'm not expert enough to do it the way you want :-)

182.232.50.87 (talkcontribs)

You mean in PHP?

Jonathan3 (talkcontribs)

"If you just need to add a button to each page maybe you could use MediaWiki:Sitenotice (see Manual:Interface/Sitenotice). If the button has HTML/Javascript that can't usually go on a wiki page you could try Extension:Widgets or similar."

What you're adding isn't really PHP (though I don't know what's in assets.php).

182.232.50.87 (talkcontribs)

I think that because this is a pure front-end module, I should not be using PHP includes in that particular case and just use a large HTML file; I don't want to take that path because the includes help me a lot, but I doubt I have a chance.

Jonathan3 (talkcontribs)

It makes sense now - "I format it in PHP because PHP supports include which I use a lot to well organize the code."

Yes I would just forget about using PHP at all. Try to get it working in wikitext. They're only buttons/links after all.

This might be an example of the XY problem!

Reply to "How to run custom PHP over MediaWiki"