$dirpath changing url when called from within wordpress file - wordpress

I have a slideshow on a php page that uses $folder = opendir($dirpath); to access images in a folder. When I enter the url of said php file all works well, however once I call this php from inside a wordpress page it no longer accesses the image folder.
I'm guessing this is due to the fact the path changes to where the php is being called to. I just can't seem to work out what I need to replace '$dirpath' with so I can access the folder url.
The tag <?php bloginfo('stylesheet_directory'); ?>/imagefolder accesses the folder when not inside a php tag, however I don't know know I can add this inside php tags.
I've tried:
$dirpath = bloginfo('stylesheet_directory')."/imagefolder";
$dirURL = "";
$folder = opendir($dirpath);
but it's not correct.
Any help would be appreciated.

From the documentation on bloginfo:
This always prints a result to the browser. If you need the values for use in PHP, use get_bloginfo().
And checking the docs on get_bloginfo(), you'll see that's better to use get_stylesheet_directory_uri(), so your code would be
$dirpath = get_stylesheet_directory_uri()."/imagefolder";
$folder = opendir($dirpath);

Related

Drupal basic page doesn’t seem to use page.tpl.php

Title says it really. Basic pages created in Drupal don’t seem to use the page.tpl.php file as a template.
If I edit the html.tpl.php file, those changes apply to every page, and it causes errors when I load a basic page.
I have also tried to copy the page.tpl.php file and name it page—basic-page.tpl.php to no avail.
Any idea what’s going on?
Also, how do I go about changing the page_top variable to include more content?
Lastly, the default page.tpl.php file has $page variables and things like $page_top and the like.
How would I call the title from the page only and the body text of a page only?
I’m using Drupal 7 and a custom sub theme.
The aforementioned files are in the template folder of the theme and I did clear caches when I added them.
Add $conf['theme_debug'] = TRUE; in settings.php and clear cache, reload page and check view source.
It will display the template file suggestions.
page.tpl.php file common for all pages. Just print anything to the tpl and run any node of basic page as well as other content type page and check if its working or not. If page.tpl.php not working for basic page only, then check your template.php file.
For print a page title just need to use following code:
<?php print $title; ?>
For print body text you need to use following:
<?php print render($page['content']); ?>
This may depend on the theme you are using. But I guess you are actually meaning page--page.tpl.php (double dashes). Which will be taken into account after you added the following snippet to your theme's template.php. Replace MYTHEME with your theme's machine name.
function MYTHEME_preprocess_page(&$variables) {
if (isset($variables['node']->type)) {
// If the content type's machine name is "my_machine_name" the file
// name will be "page--my-machine-name.tpl.php".
$variables['theme_hook_suggestions'][] = 'page__' . $variables['node']->type; // (double underscores)
}
}
See Template (theme hook) suggestions, where I also got above snippet from.
active theme debug for inspecting the template source and you get a different suggestions to user it (just avoid using node/nid).
commend drush to enable theme debug drush vset theme_debug 1

Using external file with WP_USE_THEMES set to false causing 404

I know there's a dedicated WordPress StackExchange, however, I'm getting no traction over there.
I have a file in my WordPress template I want to use for a Twitter authentication callback named oauth.php that I'd like to be accessible through htp://mydomain.com/oauth.php:
<?php
define('WP_USE_THEMES', false);
echo "test";
However the file is throwing up a 404??
This actually a issue I'd like a solution for away from just this instance.
EDIT
Using the full file path works as intended, so:
http://example.com/wp-content/themes/mytheme/oauth.php
Is this bad practise?
You could try something like this in your oauth.php:
<?php
//Instead of setting to false, try comment it out
//define('WP_USE_THEMES', true);
/** Load the WordPress Environment */
require('./wp-blog-header.php');
// Fake a 200 OK status header
status_header(200);
//The rest of your code here
?>
This should allow you to access this file at http://example.com/oauth.php and run the code inside leveraging Wordpress functionality but without your template.
NOTE: This may depend on your wordpress version.
Hope it helps!

How Link Wordpress Image Files from js

I am developing a site into sub dirctory-
sitename.com/subdirectory/
I am including an image link from my theme js file.(js/custom.js)
sitename.com/subdirectory/wp-content/uploads/2014/05/333.png
But when i move the site to main directory. I want the sitename.com/subdirectory/ will automatically converted to http://sitename.com/
is that possible? I try to used
/wp-content/uploads/2014/05/333.png
but its not working.
you should do js code in your php script before including script file.
var image1= '<?php echo content_url();?>/uploads/2014/05/333.png';
Then use this variable image1 in your js file.
This will solve your problem.
You can get the current URL by javascript from this Get current URL in web browser
Then check if current URL in subdirectory or not then you can decide what link should to pass to your image link in javascript file

Warning: Cannot modify header information when using require or require_once

I'm WordPress newbie.
I'm creating a plugin that redirect to custom login page each unregistered user access a website, let say the custom login page : custom_login.php.
I am able to create a code to redirect it but it seems no wordpress functions work in custom_login.php. So, I think I have to load something through the file. I guess wp-load.php.
Then I add some codes below at the top of the page :
<?php
require( 'd:\xampp\htdocs\wordpress\wp-load.php' );
?>
But then I got this error :
Warning: Cannot modify header information.....
I changed to require_once but still get similar error.
Some solutions of this forum threads advice to delete any whitespace. Frankly, I don't know what does it mean but I tried to delete all whitespace anyway so that the code become :
<?php require('d:\xampp\htdocs\wordpress\wp-load.php');?>
But it does not solve anything. The error is still exist.
Please help me, the expert ones.
Thanks in advance
Try inserting a system path relative to localhost, like this:
require( '/wp-load.php' ); // or just
require( 'wp-load.php' ); //
All depends on the location you are trying to include wp-load.php from.
On the other hand, you don't have to include wp-load.php if you place the file custom_login.php. in the stylesheet directory as a template or as a custom page. The way to do it is:
.1 Rename the file to page-custom-login.php
.2 Move the file to the stylesheet directory (The theme directory)
.3 Go to admin and create a new page with the title "custom login"
That's all. Now WP will treat that file as a single custom page.
They are correct - you have a space before the opening php tag in one of your files. It can be a bit tricky to find, but look hard.
If you can't find it, try looking for ob_clean() php function to help.

separate js file for front page

I am working on a drupal 7 site and am very new to this. I need to create a totally separate home page from the rest of the site. I notice the head is built in the html.tpl.php file where all the scripts are loaded. I would rather not load scripts to the rest of the site that will only be used on the front page. Also I have found that drupal adds some code to the end of my script rendering it useless.
/js/image_scale.js?lwhgie"
What is going on here and how do get better control of this?
Thanks
Just to add a couple of points:
If you want to aggregate your homepage JS (which you may or may not) then you shouldn't add the stylesheets manually to the template file. You can add them in hook_preprocess_page(), something like this:
/**
* Implements hook_preprocess_page().
*/
function MYMODULE_preprocess_page(&$vars) {
if ($vars['is_front']) {
$path = drupal_get_path('module', 'MYMODULE');
drupal_add_js($path . '/MYMODULE1.js');
drupal_add_js($path . '/MYMODULE2.js');
$vars['scripts'] = drupal_get_js();
}
}
Also I have found that drupal adds some code to the end of my script rendering it useless.
/js/image_scale.js?lwhgie"
That shouldn't render your script useless. From the comments in drupal_get_js():
A dummy query-string is added to filenames, to gain control over
browser-caching. The string changes on every update or full cache
flush, forcing browsers to load a new copy of the files, as the
URL changed. Files that should not be cached (see drupal_add_js())
get time() as query-string instead, to enforce reload on every
page request.
you could create a new template called page--front.tpl.php in your theme folder and this will be your template for the homepage. Now you can add/remove any html markup you want. for more info take a look here: http://drupal.org/node/1089656 . Also, you could "tell" drupal to use your custom template file using hook_preprocess_page() in template.php file in your theme folder. See the comments on the link I posted earlier

Resources