I have created a page template that is going to be used as homepage.
Homepage will contain three columns, each column will have excerpt of pages with particular meta_key value.
The code for this works but it is repeated each time for each column and only thing that differs is value of meta_key.
I am still new at this so I'm not sure what is a proper way to separate this code which contains both PHP and HTML?
by using PHP include statement?
by creating a function in functions.php which will contain that code?
by using Wordpress get_template_part function?
Something else?
In the end, what you want to use is what will be
Easy to understand for someone else
Convenient to you
You've grasped the DRY - don't repeat yourself - principle. This is very good. If you have to change something in the future, you don't want to have to change the same thing in multiple places (it's not convenient). So if you repeat the same thing in several different template files, you can use get_template_part. This function renders include() obsolete.
You could stick your code into a function that takes the meta key as an argument. This would be a pretty neat solution. It would be very convenient. The problem is that if someone else reads this template they will not immediately know how that function looks, since it's something custom that you made up, and they will have to find that function in order to figure it out.
So perhaps your best bet is to use a foreach loop:
foreach( array( 'meta_key1', 'meta_key2' ) as $key ) {
// Your code
}
If the code is not too long and is mostly about displaying information not so much processing it, this could be a good solution. But this only works if the output you want to generate is adjacent. Otherwise you're stuck with having to create a function, and in that case you should put it in functions.php.
If the code does a lot of processing of information you should probably put it in functions.php. The template files are for displaying stuff, and should be readable by designers with only a little PHP experience.
(btw it's not wrong for functions to contain both PHP and HTML. All template tags essentially are such functions. WordPress does not encourage truly strict separation.)
Related
First off, I'll say that I do have a lot of experience programming, but I'm incredibly inexperienced with WordPress. I'm guessing there's a correct WordPress way to do what I want and maybe I'm not using the right method, which is why I'm having trouble.
In a nutshell, my plugin reaches out to my company's server via CURL and retrieves a bunch of code (html, css, and javascript) to put on the page. It's been working fine for many years, but recently our customers have started to complain about broken functionality. Turns out it's javascript errors.
Digging deeper, we discovered that wordpress (or something) is replacing seemingly arbitrary bracketed text with seemingly arbitrary content. I see no rhyme or reason to this. For example, within the following javascript that my plugin pulled in from our server, it replaced the [i] with some random html.
Original code:
var optname = order_other_options[oname][i][optid];
After wordpress munged it:
var optname = order_other_options[oname]<div class='iconfa' style='text-align: left;'><iclass=''></i></div>[optid];
It replaced [i] with <div class='iconfa' style='text-align: left;'><iclass=''></i></div>
What's weird is that there are probably a dozen instances of [i] throughout the same javascript function, but it was only replacing four instance. ???
For another customer, it's replacing an instance of [n] with [n], yet it doesn't try to replace the brackets with &# codes in other instances within that js function.
As a temporary solution for the first customer complaint, we changed the code on our server to be [x] instead of [i] and that fixed the problem. But now I see that it seems somewhat indiscriminate about what it replaces.
I could probably write something that prepends js array variables with ersVar_ or something, something like "[ersVar_x]", but that's such a hack, I'd much rather get to the bottom of the problem.
The way my plugin works is by using add_filter(), and our customers utilize the plugin by adding a merge field [store: <account ID>], the plugin then just str-replaces that merge field with our content.
Maybe add_filter() isn't the best option? Is there another function I can use instead that would guarantee that nothing would ever attempt to modify the content my plugin generates?
And if you do recognize what's happening here, I'd be really curious to know, since none of the developers at my office could find any discernible pattern about when and why it replaces things.
Any insights are appreciated.
The issue here was the hook and the priority used on that hook.
To understand things better, please see the below explanation of the parameters in add_filter
Hook: This is the event that triggers the callback function. In Adam's case the hook was the_content.
Callback: The callback function to be triggered whenever apply_filters is called on the respective hook.
Priority: This helps you specify on how early or how late to trigger the callback function. Defaults to 10. The lower the number, the earlier the callback is called for execution. That turned out to be a problem for Adam as by using the default priority of 10, he was allowing other functions to be called after his has finished execution. It was solved by changing the priority to 999.
Parameter Count: This is the number of parameters accepted by the callback function. Defaults to 1.
I am relatively new to Drupal. We have a site and I've been asked to jump in and make some changes. I'm working on customizing the output of the Webforms module. I'm having trouble doing so because I can't seem to find a reference to the various data structures Webforms uses.
For example, I need to change something in a preprocess hook. Passed into the hook is a structure called $variables. I can see that attributes are being added to the piece I want to change, so I know I'm in the right hook. What I want to do is add something to the text. But I can't figure out where in $variables the text is so I can change it.
I'm sure what I need to change is in there, but I can't seem to get at it. All the documentation I've found on the web is either "paste this code in" or assumes you know the data structures.
So:
1. Is there a reference anywhere to these structures? $variables is one. $submission, $components are others. There are probably more. I know their contents vary widely with the specific webform, but looking for a general reference.
2. How can I see the contents of one of the structures from inside a hook? I've tried a lot of things, but no luck. Would be great to either have it output to the Apache log, or show up on the screen, something...
Any help would be greatly appreciated. It feels like there's real power here, but I can't get at it because I'm missing some basics.
I would say you need to install 2 modules to figure out what is going on...
First Devel, allowing you to use the dmp function. This will output a whole array to the message area.
And then my new favorite module, Search Krumo.
A webform is generated from large array of data and finding the bit that is relevant to you can often be difficult just looking though the dmp output. Search Krumo puts a search box in the message area allowing you to search for any instances of a string in the whole array structure. When you've found the bit that is relevant it also lets you copy the path to that array element so you can easily modify values buried deep in multi-arrays.
EDIT:
If you don't want the output on the screen but would rather log it then use Devel Debug Log. Very useful for debugging ajax requests etc.
If you just need to log simple strings not whole arrays then the dd function is useful combined with: tail -f /tmp/drupal_debug.txt assuming you have SSH access.
i use customfiel php code inside one of my views to translate a string since 2.x of views is bad at localization. i use the following php code:
echo t('Watch Video');
but the string does not appear in the "translate interface" section.
thanks for your help.
lukas
The accepted answer is wrong, as the localization script is not scanning anything. The string is registered in the translate interface as soon as it gets passed through the t() function for the first time in the non-standard language.
Therefore, for translation it doesn't matter if the code you are writing is eval'd (interpreted from the database) or exists in the source. Obviously good practice would be to keep code in files where it belongs.
This blog post describes what needs to be done to get your strings into the translate interface.
The localisation database is built by scanning the source code, looking for instances of the t() function (and Drupal.t() in Javascript).
If the code in question has been entered into a text box in the Drupal admin area, then it isn't in the source code, so it won't be picked up by the localisation process.
For this reason (and others), you should put as little code as possible into the admin text boxes. There is usually an alternative way to achieve the same thing, but even if there isn't, you should reduce the code to a minimum -- best practice would be to have nothing there except a single line function call: have it call a function, and write the function code in your module or theme. That way it will be parsed when you run the localisation.
Let's say I'm creating some app documentation. In creating a content type for functions, I have a text field for name, a box for a general description, and a couple other basic things. Now I need something for storing arguments to the function. Ideally, I'd like to input these as key-value pairs, or just two related fields, which can then be repeated as many times as needed for the given function. But I can't find any way to accomplish this.
The closest I've gotten is an abandonded field multigroup module that says to wait for CCK3, which hasn't even produced an alpha yet as far as I can tell and whose project page makes no obvious mention of this multi-group functionality. I also checked the CCK issue queue and don't think I saw it in there, either.
Is there a current viable way of doing this I'm not seeing? Viable includes "you're thinking of this the wrong way and do X instead." I've considered using a "Long text and summary" field, but that smells hackish and I don't know if I'd be setting myself up for side-effects. I'm new to Drupal.
There is the http://drupal.org/project/field_collection module but it's not yet ready. Right now you would need to implement your entity alas to do this :( not easy.
Not sure how well it would work, because it currently does a bit more (eg, forces to group pairs into categories and the keys need to be predefined) but you might want to have a look at http://drupal.org/project/properties.
You could create a these key-value fields on their own: create 2 regular fields that that can be added as often as needed.
So you have a x fields for the keys and x for the values. If this is only for you or other people it might work OK but usability wise, it's very ugly.
If you need to extract the fields from the function, to display it properly in a page template, you should propably use a different approach. Write the function with its arguemnts in a CCK field and in the template extract them as needed. The arguments are always (depending on language) in () and the different arguments are seperated by , so splitting them would by pretty easy.
i have create a category CMS,and add three terms to it (drupal,joomla,wordpress). when i create an article which belongs to drupal.now i want to show the rest drupal's articles(belongs to the term of drupal ) at the bottom of the article .is there a module to get this or how to write the code to get that.thank you.
I think the Similar by terms module will do what you want.
Coding this is not a bad way to learn some drupal coding skills. If you want to do your own, post here and I can give you pointers.
edit
You will need to implement hook_block() to create and define your block.
I suggest that you start with outputing some dummy text to prove to yourself that you have got it working.
You can then use menu_get_object() to load the object, and make sure that you only return something on node pages.
As part of your node object. Use var_dump on what is returned from menu_get_object to see what is there, there will be a taxonomy, which you can interrigate to get the appropriate term.
Then you can use taxonomy_select_nodes() to get the nodes.
Let us know you if you run into any issues.