Wordpress Plugin - Why is WP altering "[i]" (and others)? - wordpress

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.

Related

Input saves as you type to collection - how to update UI?

First - I am having a hard time formulating this question, so please bear with me, and ask for clarification and I'll try to provide as much as I can. I am just starting to learn meteor, so be patient please.
I have several inputs that save immediately as people type on them. (with a slight 300 ms delay to not overload database).
Basically, on "keyup" it goes and saves. All that works fine. However, I'd like to add a visual indicator (say a green checkmark, or a tiny "saved") when the database actually stores what they typed.
Graphically:
[___________________]
[Typed something_____] (saved)
[___________________]
I am not sure how to go about this, but it's something common, that plenty of you have already done. If I didn't care about the database feedback, I'd just use JQuery, target a class beside the input and make the checkmark or word visible after a keyup, or add it to the DOM. Fairly straight forward.The only when I am sure it has been stored in Mongodb part confuses me.
Any help would be gladly appreciated.
Thank you
Addendum with code:
Template.dibs.events({
'keyup input.name': _.throttle(function(event) {
Dibs.update(this._id, {$set: {name: event.target.value}});
$(':focus + .glyphicon-ok').css('opacity',1);
}, 300),
Can you explain where/how you would add the code? (For spinner, or the words).
Coming from JQuery I did something that I know is not the right way. This is in the client portion (I know just demo code, and it's not secure) but I wanted to know the best way leveraging meteor to do it. I already have checkmarks stating it was saved in the page, but they are all hidden, this code just makes them visible on keyup for the field.
I read through the article, and didn't quite see how I'd go about doing the intermediate step (spinner or the like) then the finalized checkmark after code is saved. I've also being going through the new 1.0 tutorial (which is great) but I'm still missing the visual indicators. It's great that meteor updates the UI if it fails in the server to reflect that it didn't save, since I am assuming success, I don't think that tapping into the Meteor.Error makes sense. Should there not be a Meteor.Success or equivalent?
Again, I apologize for the long message, I'm trying to wrap my head around this, because the technology looks very promising
Welcome to Meteor! Meteor was in fact designed (among other things) to handle just this type of situation, via a mechanism called Latency Compensation. You can read more about it at Meteor.methods.
Calling a method on the server requires a round-trip over the network. It would be really frustrating if users had to wait a whole second to see their comment show up due to this delay. That's why Meteor has a feature called method stubs. If you define a method on the client with the same name as a server method, Meteor will run it to attempt to predict the outcome of the server method. When the code on the server actually finishes, the prediction generated on the client will be replaced with the actual outcome of the server method.
You can use Latency Compensation by defining a Meteor method to save the input text to the database, with a client stub that displays a spinner instead of "saved", and "saved" when its callback is called successfully.
Alternatively, you can call the update method on the collection directly, and add a callback on the client, which will be called with (error, numberOfDocsUpdated) after the server method returns.
Read more on when to use Meteor methods and when to use client-side operations.
As Dan has said, the Latency Comp takes care of needing to do this. Another way of doing this is in a template event. You can set a session variable on keyup with the contents of the text field and in the helper, set a flag that will render the checkmark when the session variable and current user input matches.

Drupal WebForms Data Structure Reference?

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.

How to reuse code in Wordpress Page Template?

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.)

Classic ASP - Detect line number and file from which a function is called?

Our application is made in good (?) ol' classic ASP. Not ideal but it works and it's pretty stable - has been for 10-15 years. It is not particularly well documented in places, such as where a 'translation' (client-controlled piece of text) appears. All we have against a translation is a clientid and translationid, neither of which are particularly helpful. I've tried searching the (10s of thousands of lines of) core code for gettrans(1) (translation 1) and can see that doing this for another 3100 is going to be a nightmare, not to mention inaccurate as there are many functions which are called with a transid passed into them, and then they call gettrans(transid).
My last thought on this matter is the possibility that we could maybe detect, from gettrans, where a function is called from - not just the line number but the file name of the include (thankfully the includes are named usefully so figuring out where a translation is used should not be too hard!). I highly doubt that it would be possible to get the include name on the basis that includes are processed before ASP, but I'll settle for the overall filename and then we can combine the includes to get to the line of code and log the include file name.
I very much doubt this is possible and can't find anything on SO or Google. Does anyone know of any way to achieve this, or have any pointers on what I might try? Thanks in advance.
Regards,
Richard
Most you can achieve is getting the currently executing script, which can be obtained by:
Dim currentPage
currentPage = Request.ServerVariables("SCRIPT_NAME")
When inside included page it will give you the "parent" page.
However getting "callee" information is not possible with classic ASP as far as I know, so you will have to add another parameter to the function being called then change all calls to pass the parameter in order to identify where it comes from. Looks like someone did something similar and called it ASP Profiler, use it at your own risk of course. (Never tried myself)

t() function does't add the string to the translation interface

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.

Resources