A client has requested that I place an html file on their Wordpress site, but I'm having trouble accomplishing this. The html file contains custom css and javascript in addition to custom meta tags. So if I just copy and paste the contents of the file into a page, it will obviously conflict with CSS and JS loaded by Wordpress. I would like add an add_action function to recognize the page I want custom html on and then only load that and not any Wordpress or theme data.
This is as far as I have gotten on my own.
add_action('wp_enqueue_scripts', 'toggle_theme', 1);
function toggle_theme() {
global $post;
$slug = $post->post_name;
if($slug = 'dream-vacation') {
}
}
I have been able to recognize the page I want to customize, but I can't figure out how to make it a clean page and load up the custom html I need.
Stop loading the page with WordPress.
Delete WordPress page with /dream-vacation/ slug.
Save page as /dream-vacation/index.html
Related
I am creating a Wordpress plugin that adds a page that embeds a React app.
On plugin activation, a post is inserted with wp_insert_post() function, and then the load_app_template() function is triggered to set the post template.
This template contains wp_head() in the head section and my React app.
The problem is that some other plugins are loading some JS and CSS in the head that are disturbing the React app behavior. I am trying to remove all JS and CSS given by Wordpress & other plugins and add my own.
If I remove wp_head() from the post template, then I cannot register styles and scripts with my plugin through wp_register_script.
How to remove all JS and CSS from Wordpress and plugins and add my own from my plugin ?
There could be two ways :
Remove wp_head() and manually add links to my JS and CSS in the HTML template. I can do that for Jquery and Bootstrap but I don't know how to put links of JS files from my plugin directory.
Keep wp_head(), deregister all CSS and JS and add my own afterwards with Wordpress hooks. I am not able to remove some Javascript from other plugins with this solution. I tried deregistering all following this answer.
Thank you !
Try to remove unnecessary scripts and styles using wordpress hooks
(this might help you figure out how to do so: https://wordpress.stackexchange.com/questions/233140/how-can-i-get-a-list-of-all-enqueued-scripts-and-styles) if your template is used on page.
You can remove unused CSS/JS from that page. Put this code on your function.php
function remove_unused_css_js() {
// remove only from that page
if(get_the_ID() == 'Your Page ID'){
wp_dequeue_style('css-id'); // remove css
wp_dequeue_script('js-id'); // remove js
}
}
add_action( 'wp_print_styles', 'remove_unused_css_js', 100 );
Hope this will help.
I have a plugin and I need to correctly include inline css in WordPress using wp_add_inline_style() function. There is not problem to include CSS using this function in plugin, it works fine. But I have shortcodes that generates Custom CSS (different for different shortcode). For example I have shortcode that add text title with custom color to page (I generated unique CSS class name for this title DIV and add corresponding styles).
For example:
[mytitle text="Title example" color="#666666"]
And I have css that I need to inline:
.mytitle-id-4324324 h1 { color:#666666; }
As you see I can have multiple shortcodes on the same page, with different colors inside. The problem that if I use wp_add_inline_style function in shortcode it never not work. It should be inlined in "wp_enqueue_scripts" action, that does not triggered in right time from shortcode as I understand (because shortcodes loaded by WordPress AFTER wp_enqueue_scripts, when WordPress generate content). How to resolve this?
Note: I know that I can add inline CSS in HTML code or echo and it works, but I does not need this. My question how to get this work with wp_enqueue_scripts()?
As I understand I need to get one big global custom CSS from entire page shortcodes somehow, and then send it to some function that will include this big custom css for this page. But how to do this because shortcodes defenitions executed after including CSS by WordPress?
Example code for my shortcode:
function mgt_shortcode_header_block_wp($atts, $sc_content = null) {
.. some code here where shortcode show HTML output and generate custom css
}
add_shortcode("mgt_header_block_wp", "mgt_shortcode_header_block_wp");
How I can add inline custom css from shortcode? I can't do this inside shortcode (because its will not be called in wp_enqueue_scripts action) and I can't add some function after this function, because - custom css variable available only inside shortcode (I can't make it global var, because this is not good to use global vars here), I understand that I may need to pass it to some function from shortcode, something like:
function mgt_shortcode_header_block_wp($atts, $sc_content = null) {
.. some code here where shortcode show HTML output and generate custom css
mgt_add_to_inline_styles($custom_css); // how should work this function to correctly add inline styles passed to it?
}
add_shortcode("mgt_header_block_wp", "mgt_shortcode_header_block_wp");
But this will not work, because mgt_add_to_inline_styles() function will be executed only when this shortcode will be used on page in content, and this will be AFTER wp_enqueue_scripts action in any case, even if I will try to add mgt_add_to_inline_styles() to wp_enqueue_scripts action somewhere somehow.
So from my example I does not understand what code should be inside mgt_add_to_inline_styles() function to make it work correctly?
Pretty much every guide ive come across for adding php pages to Wordpress involves adding it at the theme level. e.g. How to add a PHP page to WordPress?. I want to add a new public facing page to multiple sites via a plugin. I dont want to have to add this to dozens of themes. If i can add it ad the plugin level it will work for all sites.
I have this working, but i need some way of injecting this into a theme. In order to get the sidebar and stuff without having to add custom css for each theme.
Ive started by adding a rewrite rule
RewriteRule ^mypage /wp-content/plugins/myplugin/mypage.php [QSA,L]
Then page.php contains
require_once('../../../wp-blog-header.php');
get_header();
//custom php content
//get_sidebar(); // how to get this working.
get_footer();
This also works, but the problem im having is with sidebars. Some themes dont have them and others do. Not all sidebars are 30% etc. I dont know how to build the div structure here to make it work. Some themes are 100% page width, but this looks ugly when viewed on other themes that are a fixed width. I have been able to come up with some compromises, but id rather be able to do this right.
In summery my main question here is. Is it possible to call a method that will inject html into a theme page. e.g. generate_page($html);. This method will then go to page.php of the theme and inject $html into the content area of the theme.
Edit
In an attempt to dynamically inject content into an unknown theme ive done the following
global $post;
$post->post_content = "TEST PAGE content";
$post->post_title = "Page Title";
$post->post_name = "test-page";
include get_template_directory()."/page.php";
This works for some themes and not others. Some themes will display this post just fine, but others (the default wordpres twenty fifteen theme) are displaying this post and then every other post in the database after it. Im not sure where or why its pulling all of these posts, but if i can get it to stop it looks like this will be a valid solution.
Then u could try to load a specific template page in specific case.
function load_my_template( $template )
{
if( is_page() )
$template = plugin_dir_path(__FILE__) . "dir_to/my_template.php";
return $template;
}
Or change the content that is use on loading page
function load_my_content( $content )
{
global $post;
$id = $post->ID;
if( is_page() )
{
ob_start();
include plugin_dir_path(__FILE__) . "dir_to/my_template.php";
$content = ob_get_clean();
}
return $content;
}
In your __construct()
add_filter('template_include', array($this,'load_my_template') );
add_filter("the_content", array($this,"load_my_content" ) );
add_filter("get_the_content", array($this,"load_my_content" ) );
Hope that help u.
Tell me if it's not corresponding with your question.
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'; ?
I need to include a custom PHP page in Wordpress.
So what I need to do is just to show this custom php page using the Wordpress theme installed on that Wordpress.
Does not mind which theme is up, the custom php page will have to be shown under any theme is installed in that moment.
How do I do it in Wordpress?
I am new to Wordpress development.
Thanks
Creating a custom php page that will be able to be viewed in any theme (and have the theme applied) would be considerably difficult.
Each wordpress page calls specific theme functions of that particular theme, as well as referencing files of that theme to generate header, footer, css files, javascript files, etc.. Your custom page would need to plan for all of these contingencies, for each possible theme used.
Here's a alternative solution: inject PHP code directly into a standard wordpress page via this plugin http://wordpress.org/extend/plugins/allow-php-in-posts-and-pages/
Meaning: you make a normal wordpress page, but are able to add php to it. When this page is rendered, the proper page template is used, and all the theme references are taken care of for you.
You could do this easily with a page template. WordPress allows you to create page templates which can be assigned to a page via the 'Page Attributes' panel within the page editor. These templates are php files inside your theme directory which begin with some code like (see this page in The Codex for more info):
<?php
/*
Template name: Custom PHP Page
*/
?>
<?php // begin custom PHP page ?>
Typically a template is a variation on the regular theme files (such as page.php) and would call the get_header() and get_footer() functions and have an instance of the loop. However if you simply want to use a custom PHP page, then all you need to do is create the file you want inside the current theme directory and add the above code at the very top of the file.
To output the custom PHP page on your site, you would need to add a new page via the admin area and then assign your new page template to this page.
Alternatively, if you want to include a custom PHP page inside an existing theme file, you use the code:
<?php include(TEMPLATEPATH . '/includes/file.php'); ?>
in this case your custom PHP file would be located inside a directory called 'includes' within your current theme directory.
Tim.
It's not that difficult. Here's what you need:
Once you include the main wordpress blog header, the entire armamentarium of wordpress functions is available to you, which allows you to get the active theme's directory. Once you get that, just include the header and the footer of the theme.
// If title is not displayed before loading the header, Wordpress displays "Page not found" as the title
echo "<head>
<title>Your page title</title>
</head>";
// Include the Main Wordpress blog header
include $_SERVER['DOCUMENT_ROOT']."/wp-blog-header.php";
//Now, you need to get the active theme's folder, and get a relative path to that folder
$homeurl=home_url();
$ddir= get_bloginfo( 'template_directory');
$current_theme_relative_path=substr_replace($ddir, "", 0, strlen($homeurl));
//echo "<br/>The relative path to the currently active theme is ".$current_theme_relative_path;
//Once you have the path, include the header and footer, adding your custom php code in between.
// Include the specific theme header you need
include $_SERVER['DOCUMENT_ROOT'].$current_theme_relative_path."/header.php";
// Your custom PHP code STARTS here
// Add anything you want to display to the user
echo "
<h2>
Your form has been submitted
</h2>";
// END of custom code
?>
<?php
}
// Now end with the theme's footer
include $_SERVER['DOCUMENT_ROOT'].$current_theme_relative_path."/footer.php";
?>
Was very helpfull (even if dated of 2011-13)
Also, as a thank you, i'm sharing the version i made
it's usefull if your wordpress folder is not located at ROOT
PasBin Link - wordpress custom php page
just change the value of $wplocalpath in :
// Wordpress path (if wordpress is not located at ROOT
// $wplocalpath="/Wordpress1";