Wordpress Image Paths - wordpress

I'm currently developing a new site on Wordpress to replace an old Drupal site. The site is currently being developed on 'dev.website.com', and will be moved to 'website.com' once completed.
I'd like to use a relative image path like 'images/logo.png' instead of the absolute path as it'll make the switch a lot easier, as I won't have to manually remove 'dev.' before every single reference to a file path on the site.
Is there a plugin to do this, or a setting within the Wordpress admin itself? I've looked at loads of articles but they seem to be pretty complex for what I thought could be a simple fix.
Any help would be massively appreciated!

Here is simple solution for importing images
<img src="<?php bloginfo('template_directory');?>/images/image.jpg">

I always use <?php bloginfo('stylesheet_directoy'); ?>/images/image.jpg I create a lot of child themes and this works perfectly.
You can also define a constant with define(IMGPATH, STYLESHEETPATH . '/images'); and use something <?php IMGPATH . '/logo.png';
STYLESHEETPATH is a constant that WordPress uses.

Related

Wordpress on localhost, wrong path to images

after a great deal of time, I managed to set up my wordpress on localhost running xampp. The thing is now that I don't see any pictures or posts on my local copy. Via dev tools, I realised the path to the content looked like this:
wordpress.localhost/wordpress.localhost/wp-content/...
If it wasn't for the doubled "wordpress.localhost", I'd be able to see it. What might cause this? Is this due to an error in the database? How do I fix it?
Thank you!
Best wishes
Make sure you have properly set your site name under the settings > General > Site Address : http://wordpress.localhost
Images src must be like the following if images are inside your theme folder.
<img src="<?php echo get_stylesheet_directory_uri(); ?>/images-folder/image.png" alt="">
Change the image path as per your requirements.
get_stylesheet_directory_uri() returns your active theme path.

.Htaccess rewrite URL - Only specific string

We have a current issue with a theme and until the developers of this theme fix the issue we need a quick workaround.
What the theme is doing is outputting the wrong URL for images via Amazon
What it is outputting is:
//images-eu.ssl-images-amazon.com/images/I/51heut5jiDL.jpg
What it should be is:
https://images-eu.ssl-images-amazon.com/images/I/51heut5jiDL.jpg
What we need to do is change all images outputting
What we don't want is the output already duplicating i.e. https://https:// etc etc as we already have images in place that are using the correct output.
Thanks in advance - hopefully the owners of the theme fix this quickly we just need a workaround and think .htaccess is the best bet.
This is on the wordpress platform - theme is kadence / virtue
Thanks

Relative and absolute path in wordpress doesn't work

I'm improving a word-press web site. I need to include a file to a index WordPress theme, but that happen is really frustrating for me.
If I do in the index.php wordpress theme file, everything is ok:
include('myfile.php'); // myfile.php is of course in the same folder as index.php.
And the include is done properly.
But, if I try to move "myfile.php" to , for example, the father folder, the next thing should work, but no...:
include('../myfile.php');
This is for me really frustrating because doesn't have sense...
Next thing I have tried, setting all the absolute path:
include('http://example.com/myfile.php') but again, it doesn't work.
Wordpress has shortcuts for this. Try using
<?php get_template_part('myfile'); ?>
In fact, this is the right way to do an include
the absolute path must be the path of your file system.
you can try use realpath() php function, which solves the absolute path of the file through the parameter relative path.
see documentation http://php.net/manual/en/function.realpath.php

How to make sure changes to a Wordpress plugin won't be lost on plugin update?

I'm pretty sure I've read somewhere that you can actually move the main plugin *.php file to somewhere else (I assume under your theme directory) to have it safe in case you made changes to it and your plugin updates. I tried Google but I can't find anything. Google page with good results will suffice.
I've just experienced a situation where my 2 plugins which had its layout changed and accommodated my needs and I want to make sure it doesn't happen again. Apart from having the main file in another location, is there a way to move along any CSS and JS files as well?
In Concrete5 CMS there is a nice way of doing this, by creating a new folder inside a block of an addon (may be regarded as a WP plugin), inside of which you can create copies of main file, any CSS and JS files and then you can simply edit them and choose that template for a page location you are using that block in.
I assume there is no such thing in Wordpress but how close can I get?
UPDATE: I found where I applied that advice on creating a new instance of the file then moving it to the theme directory.
The plugin in question was HL-Twitter. These are the plugin files:
admin.php
archive.php
functions.php
hl_twitter.php
hl_twitter_archive.php
hl_twitter_widget.php
import.php
widget.php
Now, this is the top contents (commented out) of the hl_twitter_widget.php:
Widget Theme for HL Twitter
To change this theme, copy hl_twitter_widget.php
to your current theme folder, do not edit this
file directly.
Available Properties:
$before_widget
$after_widget
$before_title
$after_title
$widget_title
$show_avatars
$show_powered_by
$num_tweets: how many tweets to show
$tweets: array of $tweet
$tweet: object representing a tweet
$tweet->twitter_tweet_id
$tweet->tweet
$tweet->lat
$tweet->lon
$tweet->created
$tweet->reply_tweet_id
$tweet->reply_screen_name
$tweet->source
$tweet->screen_name
$tweet->name
$tweet->avatar
$user: represents the Twitter user (ONLY SET IF SHOWING A SINGLE USERS TWEETS!)
$user->twitter_user_id
$user->screen_name
$user->name
$user->num_friends
$user->num_followers
$user->num_tweets
$user->registered
$user->url
$user->description
$user->location
$user->avatar
So I was wrong about copying the main file (in this case hl_twitter.php), but still - this enabled me to edit the file outside the plugin directory and the system somehow checks for its existence and picks it up if exists.
If this behavior something that is natively supported by Wordpress or it has been integrated in the plugin itself?
With themes, Wordpress has a concept of "child themes" which allows exactly that: to keep changes separate from main theme, in case it changes.
I haven't yet found a way to do this with plugins.
I'm using a few tactics myself:
I bump plugin version to a very high number like 99.9. This way Wordpress won't ever update the plugin.
Store my plugins in version control (i use git, but it doesnt matter), this allows you to update the plugin, run the 'diff' tool and see what changes happend. If you don't like you just revert like it would be a bad code you've written. But this approach requires a bit of skill.
Are you talking about running parts of a modified 3rd party plugin, and an updated version, at the same time?
That's not going to be possible. There is no magical method of "preserve my changes and transfer them into the new version automatically". The way to go here is doing a diff between the edited version and the update, and integrating the changes in the actual source files.
The bottom line is, if you manually edit a third party plugin, you're in for manual review (and possibly rework) once an update takes place. That's why it's usually not a good idea to extensively modify third party plugins.
Well in fact, yes! There is some kind of way.
You have to remove the to be modificated plugin's original actions/filters and then add your altereted actions/filters.
If the desired plugin is even coded in OOP you can just inherit the whole class and rewrite the wanted functions (oh sorry: "methods". we're talking about OOP ;) ). Instantiate your inherited class and rest as above.
Maybe there are better ways! I already search for a method so that the original class won't even get loaded but our altered one instead but I'm no John Carmack.

How does one inject variables into page templates from a custom Drupal module?

We've created a custom module for organizing and publishing our newsletter content.
The issue I'm running into now -- and I'm new to theming and Drupal module development, so it could just be a knowledge issue as opposed to a Drupal issue -- is how to get each newsletter themed.
At this point the URL structure of our newsletter will be:
/newsletters/{newsletter-name}/{edition-name}/{issue-date} which means that we can create template files in our theme using filenames like page-newsletters-{newsletter-name}-{edition-name}.tpl.php, which is great. The one issue I'm running into is that all of the content comes through in the $content variable of the theme. I'd like to have it come through as different variables (so that I can, inside the theme, place certain content in certain areas.)
Is there a proper way for doing this?
Edit: To answer some questions: The issue is a node (there are issue, edition and newsletter nodes) and the path is being set using hook_menu with wildcards and a router.
The best answer I could find was to add a check inside of phptemplate_preprocess_page to send the vars back to the module and have them be updated.
Like so:
function phptemplate_preprocess_page(&$vars) {
if (module_exists('test_module')) {
_test_module_injector($vars);
}
}
then in my test_module.module file I created this function:
function _test_module_injector(&$vars) {
$vars[] = call_to_other_functions_to_load_vars();
}
It seemed to work. I wish there was a way to do this without having to touch the theme's template.php file, but otherwise this works well.
If there were better documentation for template preprocess functions, Drupal would be a lot more accessible - as it is, you need to piece together the information from a lot of different explanations. One place to start is here:
http://drupal.org/node/223430
but if you take the time to work through the tutorial below, you'll find you can do most things:
http://11heavens.com/theming-the-contact-form-in-Drupal-6
This is an old post, and the OP's issues seems to have been solved.
However, just for others finding this through Google (or otherwise):
Install the 'Devel' module: http://drupal.org/project/devel
Also the 'Devel Themer' module: http://drupal.org/project/devel_themer
Use Devel Themer to go through the $content variable and find what you need to pull out.
There are a bunch of Devel/Themer docs/tuts out there, but its usage is pretty straightforward. Note, though, that some stuff in there will need to be sanitized before printing in the theme.
The suggestion to show the node as a View and then modifying the view templates sounds pretty crazy, though it'll work.

Resources