wordpress wp_enqueue_style on page that doesnt include get_header - wordpress

I have created a plugin which has it's own custom page template. I dont want this apge to use the default themes css, so have excluded get_header(), and used the following which just pulls in the wordpress functionality
require_once('../../../wp-load.php'); global $wpdb, $woocommerce;
My problem now is i am trying to enqueue a style, but as my template is not including get_header() or wp_head() it doesnt look like it is working. Is there a way around this?
I am using wp_enqueue_style like this:
function woops_styles() {
wp_register_script('packing-slip', plugins_url('/my-plugin/style.css', __FILE__));
wp_enqueue_script('packing-slip');
}
add_action( 'wp_enqueue_scripts', 'woops_styles' );

You will need to include wp_head(), so you will need to have a header. wp_enqueue_scipts hooks on wp_head().
What I would suggest if you need a diffirent styling for this page is to assign a body_class() to this page and then style that accordingly. You can use a conditional statement (is_page_template()) to only assign the body_class() to this specific page template.
If you really don't need a header to show up, simply hide it with css

Related

Removing wordpress theme from particular pages

I have a custom Wordpress theme that includes a header, footer, and a few other elements. It's great, but there are a few pages that I want to style completely differently. One of them is, for example, a landing page, where having the typical header makes no sense. How can I tell Wordpress not to use any themes on this page? Is this even possible?
You can use Page Templates
For example, if you would like to use custom header or footer for your landing page you can do following in a separate .php file and declare it as a Page Template.
I'm assuming your header and footer file name header-landing.php and footer-landing.php
<?php
/**
* Template Name: Landing Page
*/
get_header( 'landing' ); ?>
// Landing Body
<?php get_footer( 'landing' ); ?>
If you wish no header or footer then, you can just omit get_header() or get_footer() function call.
You can use conditionals to decide when to load certain stylesheets onto certain pages.
For example, in your functions.php, you can use wp_enqueue_style to load in different stylesheets. In your php, you would say something like this:
function custom_page_style_sheet() {
if (is_page() ):
wp_enqueue_style( 'custom-page-styling', get_stylesheet_directory_uri() . '/custom-page.css' );
}}
add_action('wp_enqueue_scripts', 'custom_page_style_sheet');
Try this tutorial, which is where I grabbed the example code from:
http://wpsites.net/wordpress-themes/second-style-sheet-theme/

Is there some way to insert custom archive loop to any theme from plugin?

I'm coding plugin that create custom post type with custom fields. Now I need to build custom archive/category/tag templates which should contain custom fields.
The problem is that I couldn't insert template part inside archive loop from plugin.
Is there some hook to insert custom loop inside activated theme? Something like this:
add_filter('the_content', 'change_content_in_single_post');
Now I'm using this hook:
add_filter( 'template_include', 'change_whole_cpt_archive_template' );
... but when I use template_include hook it changes whole template and I need to do something like the_content filter. That hook get template of current theme and replace standart content template with my custom template - make it part of activated theme. It makes my CPT single page template compatible with any wp theme. Now I need to replace standart archive loop with my custom loop. For example: on archive page show posts without images... It must be something that replace result of standard get_template_part() function with custom template from plugin. That's what I`m searching for...
Anyway, maybe you know some better ways to make plugin (archive template) compatible with wp themes?
Huge thanks for any help!
you need to introduce some logic to only run on your cpt archive page.
add_filter( 'template_include', 'change_all_archive_template' );
function change_all_archive_template($template){
if ( is_post_type_archive('cptname') ) {
$template= 'find the template!';
//if( file_exists ) --> look in theme 1st??
//else --> load from plugin..
}
return $template;
}
There are quite a few plugins that use this technique to look in the active theme 1st and if not found, find the file in your plugin.
I found solution in woocommerce plugin files.
So... the best way to build custom archive template from plugin is to use template_include hook and in custom template file set header and footer from activated theme:
<?php get_header();
// custom archive loop
get_footer(); ?>
To make it more compatible with any wp themes use stanard wordpress and bootstrap classes.

Wordpress body_class() loads single post css on a blog page

On my blog landing page the body_class() injects the blog style (http://madgreens.com/blog). But, when a blog page loads, the single-post style is injected (http://www.madgreens.com/blog/2015/02/chicken/).
I did upgrade 4 plugins recently but have moved them to a folder 'plugins-temp' until I sort this out.
Is there a way to force Wordpress to load the correct css on all blog pages?
That is the default behavior of body_class(). If there are other classes that you want in addition to the standard classes, you can add them by:
body_class( 'classname' )
or
body_class( array( 'classname1', 'classname2' ) )
So if there is some class you always want, just modify the body_class function in your template to include it.
For more control, you can add a filter in your functions.php file, like so:
function enhanceBodyClass($classes)
{
global $post;
// Add any classes you want to the $classes array
return $classes;
}
add_filter('body_class', 'enhance_body_class');

Is there a better way to get dynamic CSS settings into a WP page?

I'm working on a plugin where I need some of the colors to be settable in the admin.
Right now, I have these color settings saved with the WP update_option() function. Then, when I need to display a page, I use the get_option() function then embed the color codes like this:
<style>
.some_class{ background-color: <?php echo $settings->color_code;?>; }
</style>
Of course, this works. But it seems a bit clumsy because the plugin can load one of several PHP based pages. So, for each one, I have to do the above.
Is there some way to get this tag into all my plugins pages without doing it page by page?
for frontend:
add_action( 'wp_enqueue_scripts', 'custom_css', 100 );
function custom_css(){
echo '<style>css here!</style>';
}
it should print after your current css stylesheets so it will override prev. css

Wordpress: Add to wp_head() from page

In Wordpress, I have a page template that uses a specific JS file and CSS file that no other part of the site uses. On this specific template page, is there any way to add these items to the head, before wp_head is called?
I realize I could use is_page_template() in a conditional but my head file is getting out of control.
If you look here http://codex.wordpress.org/Plugin_API/Action_Reference you can see that the hook called just before wp_head is get_header
So what you need to do is: add an action called on that hook, test if you are on the page that you want and if you are add the script and the css
This would happen in a plugin (not in the theme files like header.php or functions.php) and it would look something like this:
// call the function that adds your current script
add_action('get_header', 'add_that_script');
function add_that_script()
{
if (is_page(SOME_PAGE_IDENTIFIER)) // this is the page you need; check http://codex.wordpress.org/Function_Reference/is_page on how to use this function; you can provide as a parameter the id, title, name or an array..
{
wp_register_script( 'mycustomscript', 'http://www.example.com/script.css'); // register your script
wp_enqueue_script( 'mycustomscript' ); // enqueue it to be added in the head section
wp_register_style( 'mycustomstyle', 'http://www.example.com/example.css'); // register your css
wp_enqueue_style( 'mycustomstyle' ); // enqueue it to be added in the head section
}
}
You just need to replace the id for your page and the urls to the js file and to the css. Sure, maybe you want to test some other way if you are on the right page, but I think that this shows the idea.
What about using?
if(is_page('Your Page Name')):
// do something for me
endif;
i believe u can do it from functions.php. it's tidier if you do it from there. i suggest, unless that js file is really big, you are better off including it everywhere and use wp-minify to group all js files together into one.

Resources