WordPress: prevent caching of tinymce's custom buttons? - wordpress

I'm following this tutorial on how to add a custom button to TinyMCE editor in WordPress. Trying to edit author's JS to include my functionality, yet it seems to have gotten cached. Article author has a hack for it (code snippet below), and it did work for the first time (the button is in the toolbar now), although it doesn't work for subsequent refreshes.
// "This will intercept the version check and increment the current version number by 3.
// It's the quick and dirty way to do it without messing with the settings directly..."
function my_refresh_mce($ver) {
$ver += 3;
return $ver;
}
add_filter( 'tiny_mce_version', 'my_refresh_mce');
What can I do to disable this caching?
Latest fresh WordPress localhost install, no plugins activated.

Turns out the 'no plugins activated' was not enough. Once I did a completely fresh install (without W3 Total Cache plugin), the issue disappeared.

Related

Enable WP-postratings

I've created my own wordpress theme and installed WP-Postratings plugin, but it doesn't work. I add only
<?php if(function_exists('the_ratings')) { the_ratings(); } ?>
It show rating image, but i can't rate anything.
Should I add something to the functions, maybe the reason is ajax, should I add some function?
I'm using the same plugin on my theme
Rating Buttons
if(function_exists('the_ratings')) { the_ratings(); }
Rating Avarage
if(function_exists('the_ratings')) { echo ''.expand_ratings_template('<span class="rating-images">%RATINGS_IMAGES%</span>', get_the_ID()); }
If it doesnt work you may have any javascript conflict , try to disable all plugins and try it again.You must be sure that admin-ajax.php is not blocked by server or anything else.
You should add something to the functions. That code is enough.
It usually happened because the plugin is crashed with another plugin. Disable another plugins one by one until you can rate. Then change the plugin that make the issue with the similar one.

How can I successfully use browser-sync with wordpress locally?

I'm trying to get browser-sync to work with my multipress wordpress install, for simpler mobile / responsive development.
Currently I'm having problems in that, my normal development takes place at local.example.com, and browser-sync is proxying this (via 123.456.78.9:3202, as per browser sync).
So far browser-sync loads the site, but none of my scripts or CSS are loading (although images load fine). They jsut fail with no response in the network panel.
I'm using NGINX for hosting the site, as opposed to apache.
Does anyone have any wordpress browser-sync experience? Am I missing something with the browser-sync set up? And tips for this would be super welcome. I'd love to get this as a solid part of my work flow.
The problem is to do with how wordpress handles URLs, in that it normally uses full URLs for including content and links etc.
The proxy is trying to access these on another domain and that's why it's failing.
Update
A much simple, cleaner and maintainable strategy, that also helps with development environments is to use the Root Relative URLs plugin. Adds hooks and configs similar to below, but also updates your content and editors to apply the same structure, so it's a bit more robust
Original Answer
You can add a simple hook (source: wordpress relative urls) to filter wordpress generated urls and remove the base domain so you get relative links to styles and posts etc:
$relative_url_filters = array(
'script_loader_src', //js
'style_loader_src', //css
'post_link', // Normal post link
'post_type_link', // Custom post type link
'page_link', // Page link
'attachment_link', // Attachment link
'get_shortlink', // Shortlink
'post_type_archive_link', // Post type archive link
'get_pagenum_link', // Paginated link
'get_comments_pagenum_link', // Paginated comment link
'term_link', // Term link, including category, tag
'search_link', // Search link
'day_link', // Date archive link
'month_link',
'year_link'
);
foreach ( $relative_url_filters as $relative_url_filters ) {
add_filter( $relative_url_filters, 'wp_make_link_relative' );
}
Which should clean up most of your issues and get browser-sync working nicely.
I'm still having some issues where I have more complex inclusions for images, but more or less it's working and we're already seeing how cool it is!

Hook into the WordPress Theme Customizer save action

I am facing the following issue :
I used to keep all the styling in a theme options page. When the user clicked the save button, i had a backend script that generated a css file with the changes so that they will not be output inline in each page. This has a lot of benefits, amongst them caching.
I have switched to the Theme Customizer, and everything is fine except i can't find a way to hook into the the "save" button. I would like to trigger a function that updates the content of the css file when that button is clicked in the backend.
Is this even possible ?
Thanks !
Since WordPress 3.6.0 you can now call customize_save_after.
<?php
function emailAdmin(){
mail('your#email', 'Woza!', 'You won\'t believe this but someone has updated the theme customizations!!');
}
add_action( 'customize_save_after', 'emailAdmin' );
?>
More info: http://developer.wordpress.org/reference/hooks/customize_save_after/
I'm facing the same situation. The customize_save works BEFORE the options are saved, so that's out. I've emailed Otto (ottodestruct.com) about it.
The solution I have right now is as follows:
add_action('customize_save', 'regenCSS', 100);
function regenCSS( $wp_customize ) {
checkCSSRegen(); // Checks if I need to regen and does so
set_theme_mod('regen-css', time()+3); // Waits 3 seconds until everything is saved
}
function checkCSSRegen() {
if (get_theme_mod('regen-css') != "" && get_theme_mod('regen-css') < time()) {
makecss();
remove_theme_mod('regen-css');
}
}
I also add an extra checkCSSRegen(); to my customize_controls_init function.
Again, this is a little bit of a hack. Unfortunately, it's the best I can find to do at the time.
Another option would be to use a ajax response that just pings a php file. That feels even more of a hack than this.
Another quick hack would be to do a javascript action that when the save button is clicked, it sets a timer to delay a call to a PHP file that runs the compile. That is VERY hacky to me.
The only fallback of the above, is unless the customizer is reloaded or another value saved, you may not get all the values you want.
Anyone else have a better idea?
** Update **
Just added the following request to the Wordpress team. Hopefully we'll get it squeezed in there.
http://wordpress.org/ideas/topic/do-customize_save-action-hook-after-the-settings-are-saved?replies=3#post-24853
* Update 2 *
Looks like it will be in the 3.6 release as customize_save_after. Guess a few tweets and example code can make stuff happen even with the Wordpress team. ;)
As describe by #Dovy already you can hook customize_save_after to do this now:
do_action('customize_save_after', 'savesettings', 99);
When savesettings save settings to a file it will be bad practice to do this with native php file functions (like file_put_contents()) as described here: http://ottopress.com/2011/tutorial-using-the-wp_filesystem/ by #otto.
Solution for file saving will be to use wp_filesystem. To use wp_filesystem you will need the file credentials (ftp) of the user.
customize_save_after will be called in a AJAX request and the result won't be visible. Cause of the AJAX handle you can't ask the user for the file credentials which requires a form submit.
Solution can be found by saving the file credentials to wp-config.php and add them ( temporary ) to the database. Doing this savesettings can read the credentials from the database and use them to save the file by using credentials. (this solution is described in more detail here: https://wordpress.stackexchange.com/a/126631/31759)
Not tested, but there is the action hook customize_save in /wp-includes/class-wp-customize-manager.php.
It's inside the save() function:
/**
* Switch the theme and trigger the save action of each setting.
*
* #since 3.4.0
*/
There are some other interesting action hooks (do_action) in this file that may be worth check.

How to customize Wordpress Theme before going live

Yesterday I installed a new theme on Wordpress on my self-hosted website. I am aware of the feature that allows you to preview a theme and have used it to select a new Theme that I want to install.
Problem
I do not want to interrupt normal operations of my website, but this new theme requires a lot of customization before it is ready to go. How do I do this?
My Crappy Solution
Is the only way to go about it to run a virtual server on my desktop? This seems tedious, not to mention all the errors I usually get when switching to the "real" server when doing this.
A better way?
I've been searching on SO as well as the WordPress Forum for an answer as to how to do this, but have come up short. I would have thought this is a common question. Maybe I'm using the wrong search terms [themes, customization, before installing]???
Any help is greatly appreciated! Thanks!
Ok, since your question is a pretty good one and probably not a few people are going through the same process when they decide to update their site, I decided to give a try to the get_stylesheet and get_template filter hooks. It turns out that with a very small plugin, you can easily enforce a specific theme(well in this case any logged-in visitor, but you can change this to use any logic you want) according to a specific rule/s.
Here's the code that you need to put in a file in your plugins directory:
<?php
/*
Plugin Name: Switch Theme
Description: Switches the theme for logged-in visitors, while keeping the current theme for everyone else. !!!NOTE!!! Please back-up your database prior using this plugin - I can't guarantee that it will work with any theme, nor that it won't break your site's set-up - USE AT YOUR OWN RISK(I did a quick test and it seemed to be fine, but haven't done extensive testing).
You don't need to switch to the desired theme before that - you want to keep active the theme that you will display to your visitors - the one that you will see will be used programatically.
Before activating the plugin, change the line that says `private $admin_theme = '';` to `private $admin_theme = 'theme-directory-name';` where "theme-directory-name" is obviously the name of the directory in which the desired theme resides in.
*/
class MyThemeSwitcher {
private $admin_theme = '';
function MyThemeSwitcher() {
add_filter( 'stylesheet', array( &$this, 'get_stylesheet' ) );
add_filter( 'template', array( &$this, 'get_template' ) );
}
function get_stylesheet($stylesheet = '') {
if ( is_user_logged_in() && $this->admin_theme ) {
return $this->admin_theme;
}
return $stylesheet;
}
function get_template( $template ) {
if ( is_user_logged_in() && $this->admin_theme ) {
return $this->admin_theme;
}
return $template;
}
}
$theme_switcher = new MyThemeSwitcher();
So - first of all BACKUP YOUR DATABASE! I tested locally with Twenty Eleven being the default theme and a basic framework theme as my custom theme - the theme options and navigation menus were saved properly.
Then all you need to do is to update the file(change the line that says private $admin_theme = ''; to private $admin_theme = 'theme-slug'; where theme-slug is the name of the directory in which the theme you want to use is).
Also - you won't be able to change the Front page and Posts page options, without this affecting the live site, nor will you be able to change the any shared components that both themes use(Site name, Front Page, Posts page, Posts Per Page, etc options, content and so on).
So if you have no clue whether this solution is for you - well, it depends.
If both themes are not relatively complex, then most-likely you should be able to use this hack. If they are though maybe you should do a second installation of your website as others suggested - I think that a second installation in either a sub-domain or a sub-directory would be the best option for you(simply because moving a multisite database is more complex than moving a normal WP database).
I'd setup local apache server with a wordpress installed to customize and test a new theme. When you finished customizing it then you can upload the theme to your live site and activate it. If there are settings that you need to set in the dashboard then you probably will have to adjust them again. That's one way to test/customize a theme before putting it live.
You could create a network (make WordPress multisite with define('WP_ALLOW_MULTISITE', true);, see : http://codex.wordpress.org/Create_A_Network) and then create one sub-site, then turn it "off" with a Maintenance plugin so it is not accessible to users not logged in as admin, export your posts & data from main blog, import them in sub-blog with WordPress default importer, then apply your new theme to this sub-blog and work on it. When everything satisfies you, apply the theme to the main site and deactivate subsite.

Wordpress goes blank after publishing new content?

So out of no where after clicking the publish button the page just goes blank...
The url at the top is:
http://www.brightboxstudios.com/sociabulls/wp-admin/post.php
What's good news is it still posts the content :)
Not great at coding so details rock :)
I was getting same error, when I update the site content and press update it goes to /wp-admin/post.php and this appears as blank page.
It is solved by:
I removed all the spaces at the end of functions.php file, I had included a widget and there were some spaces in between older content and new widget function, when I removed the spaces everything is working fine.
if that don't work then
- go into wp-config.php and find this "define('WP_DEBUG', false);"
change its value, if it is flase, make it true or if it is true change it to false.
And everything must work Good.
Thanks
A blank page usually means Apache failed. Check your error logs. You'll probably find a line that starts with:
PHP Fatal error:
You can also display your errors on the screen instead of looking at the log.
There will be many reasons for blank page, you have not mention version for wp, php etc.
To solve wordpress blank page issue follow the steps
disable all plugin and try (if there is an error in plugins then enable 1 by 1)
switch to default theme and try (if there is an error in theme)
rename .htaccess and param-link to default (page/post id)

Resources