How do I empty Drupal Cache (without Devel) - drupal

How do I empty the Drupal caches:
without the Devel module
without running some PHP Statement in a new node etc.
without going into the database itself
Effectively, how do you instruct an end user to clear his caches?

When you are logged as an admin (obviously, not every user of the site has to power to clear the cache), there should be a page in "Administer > Site Configuration > Performance".
And, at the bottom of the page, there should be a button (something like "Clear cached data") to clear the cache
As far as I remember, there's no need for Devel to do that, and you really don't need to go to the database, nor run some home-made PHP code.
As a reference, you can take a look at How to Clear Drupal Server-side cache

You can also use the Drush module, which allows you to use the command line to execute popular Drupal commands, like "drush cron" or "drush cache clear".

If you want to clear the cache from a module, you can use the following code.
drupal_flush_all_caches();

I have the easiest solution for that. Install admin_menu module (actually not only for this purpose, once you have installed this module, you wont regret it for sure, link: http://drupal.org/project/admin_menu). Ok, then on a newly appeared top dropdown menu hover your favicon and dropdown menu will appear, and you will see: Flush all caches menu. One click - one flush. Moreover you can flush all caches together or select what to flush: Pages, menu, themes etc. Try and you will never go back )

It would be awesome if you could just GET the behavior by hitting:
http://drupal.local./admin/settings/performance?op=Clear%20cached%20data
but you can't.
However I do want to note the URL for short-cutting through the admin menu (use the latter part):
http://drupal.local. /admin/settings/performance

On-demand clearing can be done in Administer > Site Configuration > Performance.
You should setup the cron job to run every hour (or whatever interval to your liking).
When cron is run on Drupal, all caches are cleared and rebuilt without the need for a human to manually do it.
If this question pertains to theming, you should disable the caching mechanisms (css/js aggregation) and you won't have to clear the cache data when you make changes.

HERE YOU GO:
I had to de-install the "devel" module (it was incompatible with Special Menu Items, which I needed worse), so I made my own.
Anywhere you see MODULENAME replace it with the name of your module.
STEP 1:
Add to any module (preferably one of your custom modules) in the HOOK_MENU, before the "return $items" line:
// short cut for flushing the caches:
$items['flush-cache'] = array(
'type' => MENU_CALLBACK,
'title' => t('Flush the cache'),
'description' => 'MODULENAME Custom Cache Flush',
'page callback' => 'MODULENAME_flush_cache',
'access callback' => TRUE,
);
STEP 2:
Now, in the same module file, where it's not "inside" any other function, add:
/** Page callback **/
function MODULENAME_flush_cache() {
drupal_flush_all_caches();
return 'Caches were flushed.';
}
Now, you can just go to the URL "/flush-cache" on your site to flush the cache. (After you flush the cache one last time the old way.)
STEP 3:
If you want it REALLY convenient, add the following to your page.tpl.php file. You can put it pretty much anywhere between <body> and </body>. NOTE: $my_is_test is a variable I use that's TRUE on my test system, and FALSE in production. If you don't have something like that, replace it with TRUE or FALSE to turn it on or off:
<?php if ($my_is_test): ?>
<a style="text-align:left; position:absolute; right:2px; top:20px;" href="<?=$base_path?>flush-cache" onclick="this.innerHTML = '<b><blink><big>Wait...</big></blink></b>';">flush</a>
<? endif; ?>
And voila! You have a "flush" link at the top-right corner of every page you can click on. Feel free to change the "right" and "top" amounts (or change "right" to "left" or "top" to "bottom" to put it wherever you like it. This link positioning only works on modern browsers, but it's only for you, so it shouldn't be a problem, right?

I found the following at: http://www.drupalgardens.com/content/clear-all-caches-not-working
There's another layer of caching around the site which "clear all
caches" does not affect, you're right. That's the layer that stores
the content for anonymous users.
If you want to bypass the cache for testing purposes, you can add a
junk query string to the end of your site path. For example, if you
wanted to bypass the cache on example.drupalgardens.com/foo you could
visit example.drupalgardens.com/foo?bar=baz or any other random text
set up like ?xxxxx=xxxxx.
This helped me, because I have had issues where clearing the cache under Configuration > Performance didn't seem to help.

The above code is for Drupal 6.
For Drupal 7 the flush-cache module would be as follows:
<?php
/**
* Implementation of hook_menu()
*/
function flush_cache_menu() {
$items = array();
$items['flush-cache'] = array(
'type' => MENU_NORMAL_ITEM,
'title' => t('Flush the cache'),
'description' => 'Flush all website caches to make sure it updates to relect '.
'your recent changes.',
'page callback' => 'flush_cache_custom_callback',
'access callback' => user_access('flush cache'),
);
return $items;
}
/**
* Implementation of hook_permission()
*/
function flush_cache_permission() {
return array(
'administer my module' => array(
'title' => t('flush cache module'),
'description' => t('Content admin flush cache.'),
),
);
}
/**
* Function that flushes the cache
*/
function flush_cache_custom_callback() {
drupal_flush_all_caches();
return 'Caches were flushed.';
}
Note: that you then flush it by going to:
sitename.com/flush-cache
Make sure you give them permission on the permission page. Clear cache once the "normal" way if the permission doesn't appear after turning the module on.
This is preferable when you don't want your client to get access to the admin menu but you still want them to be able to flush the cache.

The following module creates a menu item that is accessible only to users with the permission "flush cache", which this module makes available on the regular user permissions page.
/**
* Implementation of hook_menu()
*/
function flush_cache_menu() {
$items = array();
$items['flush-cache'] = array(
'type' => MENU_NORMAL_ITEM,
'title' => t('Flush the cache'),
'description' => 'Flush all website caches to make sure it updates to relect '.
'your recent changes.',
'page callback' => 'flush_cache_custom_callback',
'access callback' => user_access('flush cache'),
);
return $items;
}
/**
* Implementation of hook_perm()
*/
function flush_cache_perm() {
return array('flush cache');
}
/**
* Function that flushes the cache
*/
function flush_cache_custom_callback() {
drupal_flush_all_caches();
return 'Caches were flushed.';
}

In Drupal 8, the admin menu module isn't quite ready for use yet. And it will probably get replaced with Drupal "Toolbar". So right now there's no easy way to clear cache, without actually going to:
admin/config/development/performance
The only alternative is to add a menu item in the existing toolbar. This can be done by using this module, but as you can see, it still needs a bit of work. I got it working, but had to make a few tweaks.

use drush and this command: drush cc all
If you're using Boost to cache you need to be more specific:
drush #alias_name cc all

Related

can I have different url for custom post type in frontend and backend?

The question is straightforward. I have custom post type 'project'. In front-end for the project detail page I don't want 'project' to show in url. I mean by default project detail default url will be site_url/project/project_url but I want new url to be site_url/project_url.
I tried
'rewrite' => array(
'slug' => '',
'with_front' => false,
),
but this cause url to be changed in both backend and frontend and also page named "Projects" is throwing 404 error.
Can this be done ? Any suggestions or help is welcome.
I tried to achieve it without using any plugin but I failed. So I used plugin 'https://wordpress.org/plugins/permalink-manager/' and it helped me achieve what I wanted.
Following my comments, in short, it's not the intended initial behaviour Wordpress had in mind. Only posts or page are supposed to display that behaviour. I've just made a couple of test. None of the answer properly works, as they all have impacts on some crucial functionality.
If you're not using single-cpt.php then the accepted answer could worked for you (eg: https://wordpress.stackexchange.com/a/204210/190376) but then again I would suggest you avoid messing up the default permalinks structure.
In any cases, none of the example are talking about flushing rewrite rules upon modify the permalinks structure. Do your tests, tests all answers given in that post (eg: https://wordpress.stackexchange.com/questions/203951/), and add the following to the top of your function.php file.
<?php
/**
* flush_rewrite_rules
* Remove rewrite rules and then recreate rewrite rules.
* #link https://developer.wordpress.org/reference/functions/flush_rewrite_rules/
* Need to be removed before pushing to live.
* Can be replaced on live by: add_action( 'after_switch_theme', 'flush_rewrite_rules' );
*/
flush_rewrite_rules(); ?>
Making sure you're flushing every time is the only way to make sure your testing is accurate.

WordPress archive/taxonomy location

I'm struggling with this, despite being okay at WordPress dev.
I've created a custom post type called links
I've also created a custom taxonomy called link-type
All works fine when using archive.php in the root of the theme.
However I want links to be a child page of resources so:
example.com/links/ would become example.com/resources/links/
And clicking on a taxonomy term link for example downloads would take you to example.com/resources/links/downloads/
I'm aware of has_archive and rewrite and with_front and slug but can't understand how to use these to achieve the aforementioned structure.
As always, expert help is much appreciated.
When you register your post type, just add whatever you want to the slug in the rewrite argument, forward slashes are acceptable in slugs.
$args = array(
'labels' => $labels,
...
'rewrite' => array(
'slug' => 'resources/links',
'with_front' => true
),
);
This will give you https://example.com/resources/links/, even if you have a page already at https://example.com/resources/.
I almost forgot, you'll need to make sure your flush your rewrite rules (this can be done programatically when the CPT is registered, or you can just go to your Settings > Permalinks option page and click Save Changes to accomplish the same thing one time.

Allow a non-site administrator access to clear-cache through administrator menu, Drupal 6

I have a site-editor user role with custom permissions. Currently they can access some actions in the admin menu, but they cannot access clear-cache.
I want to expose just that option to the non-administrator (site-editor) user role. I can't find an option that granular in the permissions.
I've found some alternative options, but they involve coding, custom pages, etc. I want a pure drupal GUI option (if any exists). Not: http://drupal.org/node/152983
The reason is that site-editors enter content, but I'm caching panels and views. I need them to be able to clear the cache so they can see the changes they've made.
If you really don't want to create a custom module, there is handbook page on creating a page to clear your cache that includes a snippet to add to a page using the PHP Input format and a refinement in the comments. Keep in mind, using the PHP Input Format is usually discouraged.
It wouldn't take many minutes to create a custom form with a clear-cache button that you can give your editors access to.
The function you need to call to clear the cache is drupal_flush_all_caches
I'm not sure how this option differ from a pure drupal GUI. They are built the same way after all.
Alternatively, you could write a bit of custom code, to clear your panels/views cache when content is created or edited, which would remove this need.
use the flush page cache module?
http://drupal.org/project/flush_page_cache
You can specify what to flush and permit specific roles
If you are using admin_menu, the flush cache ability is given to the 'administer site configuration' permission, which is way bigger than needed. I am thinking of creating a small module that simply does the following:
<?php
function flusher_menu_alter($items) {
$items['admin_menu/flush-cache']['access arguments'] = array('flush cache');
}
function flusher_permission() {
return array(
'flush cache' => array(
'title' => t('Flush the cachce'),
'description' => t('This allows non admins to flush the cache'),
);
);
}
How's that sound?
Check the new CacheFlush module for clearing cache with different roles also you can create presets for clearing the cache just you need helping to save time on development process.

Administration menu

I'm looking for some beginners resources for drupal. I've been writing my own module as a way of teaching myself the basics of development as I like the idea of having Drupal to go to when Wordpress can't manage. (Although wordpress is fast becoming as accomplished as Drupal, but that's not what the questions about.)
I have the module showing up in the enable/disable screen and the module.install file works a treat. What I'm stuck on is generating an admin area to edit entries in the table I'm using. I have a module_admin_settings() function in module.admin.inc, plus module.menu () in module.module.
But it isn't appearing in the menus and I'm stumped as to why that is. So I'm hoping someone knows a good tutorial that explains how to generate admin options.
If you want to become serious with Drupal development, I would advice you to go through the Pro Drupal developemnt book. It has been the single most helpful Drupal book I have ever read.
As for your question, to create a admin section is not much different from creating a view some where. What you need to do is.
Implement hook_menu to register the url you want to use, in your case admin/settings/name should be fine
Implement a call back function that should be called when a user go to the your. In your case you don't need to implement one as you should use drupal_get_form, but instead you need to create the form that the user should see using the form API.
You need to place this info in your hook_menu like this:
function modulename_menu() {
$items = array();
$items['admin/settings/modulename'] = array(
'title' => 'Menu item',
'description' => 'The description of the menu item. It is used as title attribute and on the administration overview page.',
'page callback' => 'drupal_get_form',
'page arguments' => array('form_id'),
'access arguments' => array('administer modulename'), // access restriction to admins only
);
return $items;
}
Then you need to implement validation and submit handlers for your form, is you use a system_settings_form, you can get a lot of the work done for you.
Whenever you make changes to hook_menu, you need to clear the (menu) cache, as Drupal caches this information to optimize performance.
That's it. Now you don't need to use different files in your module. If the module is small all could just go into the .module file. But for bigger modules, you can place some of your code in .inc files to organize your code a bit. Fx modulename.admin.inc for your admin callbacks/functions, modulename.page.inc for your regular callback/functions etc.
http://drupal.org/node/206761 is what I use as a starting point.

Creating "ON/OFF News Links" functionality for a block created with View Module

I'm a drupal newbie who needs some advice...
I have a news list block at homepage, created with View Module. It is listing all added news' title and link. Everything is cool so far. Now I need to add an ON/OFF option at admin side for homepage news block. When the setting is ON, it will work as it is. When it is OFF, only the titles will be listed with no linking to news detail page.
So, now where should I add this ON/OFF option? I have only add/edit/delete pages for each news, there is no common news page to add such option. Should I create an admin page with such ON/OFF option in? Also I think I need to create a db table to keep this ON/OFF status. and controlling this value at homepage block, if it is 1 or 0, and displaying links according to db value :/
it looks too long way
Create db table
Create an page with ON/OFF option in
add php codes to update db for admin's choice
get the db value in homepage block to display links, etc.
is there any shorter, better way to do what I need?
Appreciate helps so much!!! Thanks a lot!!
You definitely don't need to create any database tables for something like that. If you want a basic admin page, you will need to write a simple module though. First follow this quick start guide for setting up a basic module. (Note: You don't need to add those database queries in your .install file)
Once you have your module enabled...
1) In your mynewmodule.module file, add a menu entry to tell Drupal where your admin page can be accessed:
function mynewmodule_menu() {
return array(
'admin/settings/mynewmodule' => array(
'title' => 'My New Module',
'description' => 'Change settings for news display.',
'page callback' => 'drupal_get_form',
'page arguments' => array('mynewmodule_admin_form'),
'acces callback' => 'user_access',
'access arguments' => array('administer site configuration'),
),
);
}
2) Also in your mynewmodule.module file, add a function to create the form you just referenced in the menu entry:
function mynewmodule_admin_form() {
$form = array();
$form['mynewmodule-on-off-switch'] = array(
'#type' => 'checkbox',
'#title' => t('Enable news links'),
'#description' => t('Control whether news items are linked to stories'),
'#default_value' => variable_get('mynewmodule-on-off-switch', 1),
);
return system_settings_form($form);
}
3) Clear your cache to make Drupal recognize your admin page (you need to clear any time you make changes to mynewmodule_menu()). You can clear it here: admin/settings/performance
4) Visit admin/settings/mynewmodule to see your admin form. The way it works is when you save the configuration, Drupal will save a variable called "mynewmodule-on-off-switch" (same name as the array key in the form) to the variables table in the database. You can get this value anywhere by using variable_get().
create a form at admin/settings/on-off-switch.
on the form submit function, variable_set('on/off switch', $value) (try using booleans for the value).
then on the view theme, check for variable_get('on/off switch', $default_value) before printing the links.
Drupal's weakness, IMHO, is the sheer number of admin settings to configure to get a site up, and you don't want to be adding to that.
What I would do is to have the view expose two different blocks, one with the full view, one with the abbreviated view. Then all the configuration can be done through the block interface, which will be much more flexible in the long run. By, for example: using wildcards or php code for block visibility; showing different views to users with different roles; allowing visitors to control which view they see; exposing the two views to the theming engine more cleanly; and integration with any other module that works with blocks.

Resources