Wordpress calling an HTML file from PHP - php

I am trying to modify a theme on wordpress. The theme shows a slider on the front page, which reads from the featured.php file.
I have gone into the featured.php and removed all the PHP code. I can inject my custom HTML into the featured.php and the pages display properly, however I want the page to display from a wordpress page.
So i tried to:
<?php
$html = file_get_contents('http://www.mydomain.com/homepage-featured');
?>
The above link corresponds to a page i created in wordpress. So i want to be able to inject my HTML into that page and then when you load my homepage, the PHP tells the browser to display the contents of this URL.
The above code doesn't work.
Thanks for the help.

file_get_contents() - as its name suggests - reads a file in but does not print it. You need to echo your $html variable. A better way is to use require() or include() but if I were you I would put my custom file on the same server so that way you don't have to use the file from a remote location thus sparing network traffic.

I think you have better to use the include function.
The file_get_contents you are using would generate an HTTP request, so it would make your script slower. I think it would be a good idea to put the HTML file on the same server if possible.

Have you tried
<?php
require('http://www.mydomain.com/homepage-featured');
?>

If my understanding is correct, you are trying to use a template (featured.php) for a different page other than the front page.
To do so, just change the Page Template of the page(the separate page which is # www.url.com/myhomepage). You can change this # Dashboard > Pages (Click Edit link in the required page) > Edit Page > Page Attributes meta box (available in the rightside below Publish) > Template. Change the template of this page to "Featured".
(I assume that your code in file feature.php has Template Name: Featured at the top)

Related

How to add(include) elementor created page in theme file 404.php

I created a page using elementor(named as: 'Error-page'), currently using linoor theme. What I want is to load this 'Error-page' via theme's default 404.php file (NOT using any plugin). Should I just type in 'include Error-page.php' in 404.php file to make it work?
Meta: My goal is to show my custom error page which I created using elementor(named as: Error-page) when invalid url is typed. One way of doing it is using plugin(I prefer not to do this for the time being), 2nd way could be somehow including this custom page in theme's default 404.php file
Note: Im new to wordpress
after thinking about your question, i have 2 solutions for you:
1. if you do not have code knowledge
This way is simple and safer, but have limits than other way - it's not using your template but redirect viewer to your template page. Any way, the same result
Build the 404 template with Elementor and publish it
Open your theme's 404.php file
In the very first top of the template file **(below <?php tag) paste this code:
wp_redirect( '/your-template-slug' );
exit;
Save it and you're done
2. If you have basic code knowledge:
This way is a litle complex, but it alow you to using not for 404 file - but other templates (archive, categories...)
Install "Anywhere Elementor" plugin
Build the 404 template as you do with Elementor
Get your template php shortcode
Edit the theme's 404.php file to echo out your template
In case you need more detail guide, i have record a step by step guide video here

Wordpress Adding Custom HTTP Header To One Template File

I need to add a header Access-Control-Allow-Origin: * into one of my template files. How would I do this I would like it to be for just this specific template so that its not site wide,
I think you're looking for is_page_template();
<?php
if(is_page_template('your-page-template.php')) { //Change this to the path to your wordpress template
//Add your code here...
} ?>
you can create a custom template page and call the custom Header on this template.
To create a page and apply a page template
use this code to create page template
First you need to have a single page. There are several ways to do it, one is to create a page in wordpress, then use the ID and create a file page-ID.php in the theme folder. Other way is to create a template file which you can select from the side menu in the wordpress editor.
In that page, you can add the php header and the code you want to show.

Am I using Wordpress incorrectly?

I am new to web dev with WP. I just realized that I made my front page (it has a form to be filled out by a user) by putting the code for it in a php file (through CPanel) and using <?php include() ?> to use that php file in my index.php. However, I just noticed that WP has a Pages section where I can add code by simply surrounding it with <code></code>. What is the best practice here? Am I under-using the CMS that is Wordpress by not using this Pages section? Or is it ok to not use it?
you can use some pulgins to use php code in pages like "allow-php-in-posts-and-pages" . and if you want some php file then you have create a template for that and that temmplate should be add in wp-page .

Wordpress Get URL Footer File

I need to access the footer inside a wordpress theme via a url. Ideally it would just render the footer data and nothing else, alternative suggestions welcomed though.
To add some context, the data will be fetched through the url and a script will read the markup. This data will then be cached and available through Magento where it is needed to be displayed..
Is there a url path that will display it or should I make a new page that can be called via the base-url+the-page-name???
There's nothing built in to do that. You could use the AJAX API in your theme or a plugin and accomplish it.
You hook into an action that gets sent as a URL or POST data parameter and then make a request to yoursite.com/path/to/wordpress/wp-admin/admin-ajax.php.
<?php
add_action('wp_ajax_so20495429_display_footer', 'so20495429_display_footer');
add_action('wp_ajax_nopriv_so20495429_display_footer', 'so20495429_display_footer');
function so20495429_display_footer()
{
get_footer('ajax');
}
This will work for all users, logged in or not, despite the wp-admin URL. Here is code above wrapped up in a plugin.
what you can do is make a wordpress custom page template.
If you dont know how heres a little tutorial : http://www.expand2web.com/blog/custom-page-template-wordpress/
Now you want the custom page template to only load the footer (add the html, head and body opening tags yourself)
if you create a new page with your page template seletced it will only output the footer.
Hope you can get it to work
Greetings
merijn
What about $footer_url=get_template_directory_uri().'/footer.php'; ?

Adding a javascript to all pages in OpenCart

I'm new to OpenCart and don't have any experience with PHP, so I have a question. I want to add the following JavaScript to hide the url bar on mobile browsers
// When ready...
window.addEventListener("load",function() {
// Set a timeout...
setTimeout(function(){
// Hide the address bar!
window.scrollTo(0, 1);
}, 0);
});
However, I can't find a way to insert this so this code will be executed on all pages in OpenCart. Where should I put this code?
save your script to a file, say 'catalog/view/javascript/myscript.js'
Then add
$this->document->addScript('catalog/view/javascript/myscript.js');
to the catalog/controller/common/header.php some place before this line:
$this->data['scripts'] = $this->document->getScripts();
You could also just place your script inline into catalog/view/theme/{theme name}/template/common/header.tpl using normal html markup.
Looking at the theme documentation, I believe you want to edit the following file:
catalog/view/theme/{your-theme}/template/common/header.tpl
These templates (header, footer, etc) should appear on all pages.
You don't need to go through this trouble especially if you don't have access to FTP. All you need to do is just go to admin panel > design > theme editor > and choose the respective parts to insert the codes. If is footer then just choose footer.twig.
After adding the codes, click Save and you will see the changes immediately. If add the codes directly to the file on FTP, it won't work.
Tried and tested on OpenCart 3
To add a script like that, just go to admin panel > design > theme editor > select "common" > footer and in the end of the file (after </html>) add the script.
If you want script to all your pages of the OC just add it before footer tag in footer.tpl or footer.twig

Categories