How to get .js and .css files and replace them - wordpress

I'm creating a Wordpress plugin.
I've created the basis for this and all is correctly set up.
I've tried looking at this and this to see if I could request and get all of the scripts (.js files) from the header and footer.
I thought that was the best function but the result is not happening right.
I basically want to get the JS and CSS files and replace with another string.
So let's say this is my page:
<!DOCTYPE html>
<html>
<head>
<title>Page</title>
<link rel="stylesheet" href="http://www.example.com/mycss.css" />
</head>
<body>
<script type="text/javascript" src="http://www.example.com/myscript.js"></script>
<script type="text/javascript" src="http://www.example.com/another_script.js"></script>
</body>
</html>
My function has to look for the .css and .js files and replace the string with other stuff. So that the result would be:
<!DOCTYPE html>
<html>
<head>
<title>Page</title>
<mytag>Here was contained a CSS from http://www.example.com/mycss.css</mytag>
</head>
<body>
<mytag>Here were contained 2 JS from http://www.example.com/myscript.js and http://www.example.com/another_script.js</mytag>
</body>
</html>
EDIT
I actually have to get the name and path for those files.
I think I can use a regexp but I don't know how to load it using get_header and get_footer (if those are the right functions)

It would make much more sense, rather than removing the relevant markup after receiving it from get_header() and get_footer() functions, to dequeue all styles and/or scripts so they are never printed there in the first place.
You can get the path for the file by looking back at the $wp_styles/$wp_scripts objects' registered property. You can then go on to get the filename using basename():
function dequeue_all_styles() {
global $wp_styles;
foreach( $wp_styles->queue as $handle ){
wp_dequeue_style($handle);
$src = $wp_styles->registered[$handle]->src;
$filename = basename($src);
}
}
add_action( 'wp_print_styles', 'dequeue_all_styles', 100 );
function dequeue_all_scripts() {
global $wp_scripts;
foreach( $wp_scripts->queue as $handle ){
wp_dequeue_script($handle);
$src = $wp_scripts->registered[$handle]->src;
$filename = basename($src);
}
}
add_action( 'wp_print_scripts', 'dequeue_all_scripts', 100 );
If you have other styles and scripts to put in their place, simply use wp_enqueue_style() and wp_enqueue_script().

You can access the enqueued script and styles by using $wp_scripts and $wp_styles global variables.
See the class reference for WP_Styles and WP_Scripts for available methods and properties.

Related

Inserting <link/> tag into <head> in WordPress

I'm trying to insert html into the head of my WordPress site, the plan is to insert a link tag to preload my main css file so that when it is loaded using wp_enqueue_style it is already preloaded. But I'm not entirely sure how to do this, I've seen a few similar issues and have tried a few approaches with no luck, currently I am trying:
function preload_styles() {
$href = 'styles/main.css';
echo `<link rel='preload' as='style' id='main-css' href='$href' type='text/css'/>`;
}
add_action( 'wp_head','preload_styles', 10);
But it seems to be doing nothing... Nothing is being inserted into the head. Any help on this would be greatly appreciated.
You are messing with a single quote and double quote. Try the below code.
function preload_styles() {
$href = 'styles/main.css';
echo '<link rel="preload" as="style" id="main-css" href="'.$href.'" type="text/css/">';
}
add_action( 'wp_head','preload_styles', 10);
Tested and works

I can't enqueue css using a function in WordPress

My stylesheet is enqueued if i use
<link rel = "stylesheet" type = "text/css" href = "<?php echo get_template_directory_uri();?>/style.css">
but it's not included when i use
function include_css(){
wp_enqueue_style('main_css',get_stylesheet_uri());
}
add_action('wp_enqueue_scripts','include_css');
You may correct the code like this :
function include_css(){
wp_enqueue_style('main_css',get_template_directory_uri().'/style.css');
}
add_action('wp_enqueue_scripts','include_css');
The issue is that you are trying to enqueue the css file without specifying it's complete url the code you use i.e
> wp_enqueue_style('main_css',get_stylesheet_uri());
This means that you are adding the style.css file which is always on the root directory but still you are giving name as main_css first make sure which css you want to add then someone will able to properly answer your question and the css you are trying to add is in which directory.
Please refer to this link get_stylesheet_uri
To enqueue a stylesheet use this in functions.php
1.we need to register our stylesheet.
2.we should enqueue it.
3.Hook to an action
4.call the function where you want to enqueue it.
function include_css()
{
wp_register_style('main_css',get_template_directory_uri().'/style.css');
wp_enqueue_style('main_css');
}
add_action('wp_enqueue_scripts','include_css');
call,
<?php wp_head();?>
in the place you want to enqueue it.(mostly in header.)
Please add this code in theme's functions.php
function enqueue_scripts() {
wp_enqueue_style('style', get_stylesheet_directory_uri() . '/style.css', array());
}
add_action( 'wp_enqueue_scripts', 'enqueue_scripts' );

Add wp_header depending on wp_content in Wordpress Plugin

I am developing a Wordpress plugin to run a certain javascript function based on some parameters.
The first step is to include the javascript file from my server. Easy:
add_action('wp_head', 'my_plugin_head');
function my_plugin_head(){
echo '<script type="text/javascript" src="http://www.my-server.net/js/w.js" ></script>';
}
The second step is to change certain text in the WordPress body to the javascript function...something like:
add_filter('the_content', 'plugin_text_replace');
function plugin_text_replace($text){
$text=preg_replace('blah', 'blah');
return $text;
//I Am still researching how to setup the preg_replace.
//It will look for something like plugin_call[1, 40, 60]
//And Change it To jsFunction(1, 40, 60);
//Bonus for anyone who can help me with that :)
}
In any case, I realized that I want the my-server.net javascript included ONLY if needed (or in other words, only if the preg_replace found a match). This is problematic to me because I can't find anyway to add a script to the <head> tag without using an action on wp_head, which does not have any reference to the body of the text.
How can I add a header ONLY if a certain preg_match is found?
To add javascript the best way is using wp_enqueue_script(), like this:
function my_scripts_method() {
wp_enqueue_script('my_java_function');
}
// For use on the Front end (ie. Theme)
add_action('wp_enqueue_scripts', 'my_scripts_method');
Inserting code into the <body> can be achieved like this:
function wp_body() {
do_action( 'wp_body' );
}
add_action( 'wp_body', 'my_body_function' ) ; //
// In "header.php" place something like this after <body>:
if ( function_exists('wp_body')) wp_body(); ?>

Codeigniter: css is not working and is eating my code [duplicate]

In my web application using codeigniter. I am trying to use base_url() function but it shows empty results. I have also used autoload helper through autoload file, but then too it doesn't seem to work. Also I had defined base constants but all in vain.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo $title; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" href="<?php echo base_url();?>/css/template/default.css" type="text/css" />
<script type="text/javascript">
//<![CDATA[
base_url = '<?= base_url();?>';
//]]>
</script>
</head>
In order to use base_url(), you must first have the URL Helper loaded. This can be done either in application/config/autoload.php (on or around line 67):
$autoload['helper'] = array('url');
Or, manually:
$this->load->helper('url');
Once it's loaded, be sure to keep in mind that base_url() doesn't implicitly print or echo out anything, rather it returns the value to be printed:
echo base_url();
Remember also that the value returned is the site's base url as provided in the config file. CodeIgniter will accomodate an empty value in the config file as well:
If this (base_url) is not set then CodeIgniter will guess the protocol, domain and path to your installation.
application/config/config.php, line 13
If you want to use base_url(), so we need to load url helper.
By using autoload $autoload['helper'] = array('url');
Or by manually load in controller or in view $this->load->helper('url');
Then you can user base_url() anywhere in controller or view.
First of all load URL helper. you can load in "config/autoload.php" file and add following code
$autoload['helper'] = array('url');
or in controller add following code
$this->load->helper('url');
then go to config.php in cofig folder and set
$config['base_url'] = 'http://urlbaseurl.com/';
hope this will help
thanks
Check if you have something configured inside the config file /application/config/config.php e.g.
$config['base_url'] = 'http://example.com/';
I think you haven't edited codeigniter files to enable base_url(). you try to assign it in url_helper.php you also can do the same config/autoload.php file. you can add this code in your autoload.php
$autoload['helper'] = array('url');
Than You will be able to ue base_url() like this
<link rel="stylesheet" href="<?php echo base_url();?>/css/template/default.css" type="text/css" />
Apart from making sure you have set config/autoload.php:
$autoload['helper'] = array('url');
Change application/config/config.php from:
$config['base_url'] = 'http://example.com/';
Become a dynamic base url:
$config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https": "http");
$config['base_url'] .= "://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
And using the host from php to run it on local, below is just an example port.
php -S localhost:2000
If you don't want to use the url helper, you can get the same results by using the following variable:
$this->config->config['base_url']
It will return the base url for you with no extra steps required.
Load url helper in controller
$this->load->helper('url');
Anything if you use directly in the Codeigniter framework directly, like base_url(), uri_string(), or word_limiter(), All of these are coming from some sort of Helper function of framework.
While some of Helpers may be available globally to use just like log_message() which are extremely useful everywhere, rest of the Helpers are optional and use case varies application to application. base_url() is a function defined in url helper of the Framework.
You can learn more about helper in Codeigniter user guide's helper section.
You can use base_url() function once your current class have access to it, for which you needs to load it first.
$this->load->helper('url')
You can use this line anywhere in the application before using the base_url() function.
If you need to use it frequently, I will suggest adding this function in config/autoload.php in the autoload helpers section.
Also, make sure you have well defined base_url value in your config/config.php file.
This will be the first configuration you will see,
$config['base_url'] = 'http://yourdomain.com/';
You can check quickly by
echo base_url();
Reference: https://codeigniter.com/user_guide/helpers/url_helper.html
Question -I wanted to load my css file but it was not working even though i autoload and manual laod why ? i found the solution =>
here is my solution :
application>config>config.php
$config['base_url'] = 'http://localhost/CodeIgniter/'; //paste the link to base url
question explanation:
" >
i had my bootstrap.min.css file inside assets/css folder where assets is root directory which i was created.But it was not working even though when i loaded ?
1. $autoload['helper'] = array('url');
2. $this->load->helper('url'); in my controllar then i go to my
I encountered with this issue spending couple of hours, however solved it in different ways. You can see, I have just created an assets folder outside application folder. Finally I linked my style sheet in the page header section. Folder structure are below images.
Before action this you should include url helper file either in your controller class method/__constructor files or by in autoload.php file. Also change $config['base_url'] = 'http://yoursiteurl'; in the following file application/config/config.php
If you include it in controller class method/__constructor then it look like
public function __construct()
{
$this->load->helper('url');
}
or If you load in autoload file then it would looks like
$autoload['helper'] = array('url');
Finally, add your stylesheet file.
You can link a style sheet by different ways, include it in your inside section
-><link rel="stylesheet" href="<?php echo base_url();?>assets/css/style.css" type="text/css" />
-> or
<?php
$main = array(
'href' => 'assets/css/style.css',
'rel' => 'stylesheet',
'type' => 'text/css',
'title' => 'main stylesheet',
'media' => 'all',
'index_page' => true
);
echo link_tag($main); ?>
-> or
finally I get more reliable code cleaner concept. Just create a config file, named styles.php in you application/config/styles.php folder.
Then add some links in styles.php file looks like below
<?php
$config['style'] = array(
'main' => array(
'href' => 'assets/css/style.css',
'rel' => 'stylesheet',
'type' => 'text/css',
'title' => 'main stylesheet',
'media' => 'all',
'index_page' => true
)
);
?>
call/add this config to your controller class method looks like below
$this->config->load('styles');
$data['style'] = $this->config->config['style'];
Pass this data in your header template looks like below.
$this->load->view('templates/header', $data);
And finally add or link your css file looks like below.
<?php echo link_tag($style['main']); ?>
This All Directly Access Function Will Be Loaded Through Helper Class Only.
Like URL, Security, File all Are Helpers and You can Also Load Custom Helpers.
config/autoload.php
$autoload['helper'] = array('url', 'file', 'authorization', 'custom');

Drupal - print the current/active sub theme

Im trying to get the path to the current theme (which is a sub theme) but I am having issues.
I have seen these 2 sites:
http://venutip.com/content/getting-path-your-subtheme
http://www.website-assistant.co.uk/web-developer/path-subtheme-drupal
Which has led me to this: (as an example)
template.php
function phptemplate_preprocess_node(&$vars) {
global $theme_key;
$path_to_theme = drupal_get_path('theme', $theme_key);
}
?>
page.tpl
<link rel="stylesheet" href="/<?php print $path_to_theme; ?>/css/print.css" media="print" type="text/css" />
But it doesnt work. What am I missing? I am using Drupal 6.19.
A.
In your phptemplate_preprocess_node function, you need to add the $path_to_theme variable to the $vars array:
function phptemplate_preprocess_node(&$vars) {
global $theme_key;
$vars['path_to_theme'] = drupal_get_path('theme', $theme_key);
}
You are also able to add print CSS via the theme's .info file by adding a line like this:
stylesheets[print][] = css/print.css
The advantage of this approach is that your CSS files will be aggregated properly when CSS aggregation is enabled.

Resources