How to invoke a URL from a PHP plug-in?
-
Need to create a PHP plug-in that can invoke a specific URL – https://…. – out on the internet. How can this be done?
-
I have no idea what it is you intend to do.
I take it by invoke you mean a redirect?
WordPress has functions like wp_redirect() and wp_safe_redirect(). It seems they create a html header tag that initiates a redirect, so this happens upon page load.
A redirect after page load can only be done using javascript or per a user action (form post, link click, etc.).
What I need to do is to, from a PHP program that is a WP plug-in, redirect out of WP and off to a full URL which I will specify.
I have never written a WP plug-in and my PHP is very slight. Can you share the skeleton source for a PHP plug-in with redirection to, for example, https://www.jthweb.com/KSR/KSRsearch.php, just as a test.
I need to do some more things, but if I could get this much working I would be one happy camper!!
If I should do this in JavaScript instead of PHP, so be it. In fact, I would rather do it that way if at all possible because I have much more experience writing JavaScript than I do PHP. Again, samples would be much appreciated.
You are basically asking me to provide you with information that is readily available. The plugin skeleton you ask is just the barest skeleton you could need, which you can find in
wp_content/plugins/hello.php
.You apparently just want to define a function that will output some text with the html/javascript code you need.
From what I can see, as soon as you have a php file in the
plugins
directory with the proper header information, it will get loaded and that function will get defined/loaded.Then you will be able to call that function from your own php files.
Perhaps you would need some code to be executed at certain events, which you would do with
add_action()
calls, also featured in that sample plugin.Well, I posted this thread a couple of months ago but I got sidetracked and I’m just now getting back to it. The problem I need to solve has not changed. I need to, from a PHP plug-in in WordPress, to cause a dynamic URL to be built and then executed (redirected)
The destination for the URL will be the same each time but there are standard HTML parameters that must also be included. The general form for the URL would be: https://www.xxx.com?key=A114C5F0
The key is computed each time and it is assumed that this plug-in can be triggered in various ways in the WordPress web site. Here’s is a prototype of such a plug-in:
`/*
Plugin Name: Database Search
Description: Search Database
Author: Joe T. Hall
Author URI: https://JoeTHall.com/
Plugin URI: https://www.xxx.com
Version: 1.0
*/
<?php
function redirect_to_KSRsearch() {
header( ‘Location: https://www.xxx.com/yyy.php’ ) ;
}
add_action ( ‘wp-head’, ‘redirect_to_xxx’ );’Would it be possible for me to enter into an email conversation with someone that might know how to accomplish this task?
Thanks
Joe
Again, you cannot set the header from PHP after it has started outputting other text. Of course you can use PHP to output JavaScript code that can do it for you.
In other words, it would output user interface elements that are tied to JavaScript functions that you defined in some
.js
file, probably.It would occur to me that loading a generic PHP file with a parameter that then does the redirect (using that plugin you supplied) is not very useful. Simply because if you are loading that PHP file with a parameter, you could also load the redirect you want, directly.
A PHP redirect is really something that you can only do, or so it seems, without any user interaction, that is, as part of the input parameters (either GET or POST) of your PHP script.
Usually such a thing would indicate a form of error condition. It cannot be the result of user action. Unless it is the result of e.g. a login script that does a redirect after processing the login credentials.
It seems to me that what you want foremost is JavaScript redirects. Back in the day I simply used “document.location = “xxx”” for that.
What you then want is a bit of code that outputs this Javascript. It appears to me as though you want this computation and URL construction to be done by PHP.
The JavaScript to output would not need to be more difficult than:
document.getElementById("clickMe").onclick = function () { document.location = "url"; };
You could output this JavaScript anywhere after the element itself has been defined in HTML.
<?= "<script>document.getElementById(\"$element\").onclick = function () { document.location = \"https://www.xxx.com?key=$key\"; }; </script>" ?>
Alternatively, you could use jQuery to do more flexible selection:
$("#elementid a").on("click", function(){ document.location = "url" });
If you want the key to be returned ‘live’ from a function, it would turn out something like this:
<?= "<script>$(\"$element\").on(\"click\", function () { document.location = \"https://www.xxx.com?key=" . key_gen($element) . "\"; } ); </script>" ?>
That is all I can do for you right now. If you want to “engage in email conversation” what you are really asking for is a professional service, which will require payment. If you are seeking such a service, you should not be asking for help on a forum. Forums are for mutual exchange in which both parties benefit from the exchange out of personal interest in either the topic or the problem at hand.
Meaning, on a forum you can only expect to obtain guidance on how to do something yourself. A responder will only invest more time and energy if he/she feels you are deserving because you have done everything you can do yourself, meaning you have made an investment yourself in your own problem resolution.
But currently, if I may be so bold, you sir are asking people to invest while you have not invested very much yourself. The replies and suggestions I am giving you here are in direct proportion to your own investment (which comes down to explaining what you actually want). Since you have not shown us any of the HTML structure you want to produce or use or output, you have not told us where this key is going to come from, how many such user elements there are going to be, whether the key can be computed based on the page (post id) alone, or additional form inputs, etcetera etcetera etcetera, I would hold that your own investment in your own problem resolution is currently rather limited.
If you, good sir, are then asking for people not only to write that code but also to obtain all the information from you that is necessary for writing that code, while you are not giving this forum any of that information, it means you are requesting a professional service.
Which is your good right. But you are, if I again may be as bold, not really owning up to that, possibly, as it could very well be the case, in such situations, simply because you are reluctant to make that monetary investment.
But you need to invest either with your own time and energy, or with your money, which is another form of time and energy. But you can be ascertained that whatever investment you make, it will pay off. In direct proportion.
So to directly answer your question, no, it would generally not be possible to engage into an email conversation with someone that might know how to accomplish this task, unless someone now feels sorry for you because I have said these things ;-).
But you are simply not getting a free ride just like that. Sorry.
I have been properly chastized for my naive approach. Please accept my apologies!
I assure you that I was not looking for a “free ride”.I am almost totally new to WordPress and its plug-ins and I’m trying hard to bootstrap myself with enough information to move forward.
I was hoping to collect the little pieces of information that I need to put this together.Since you have not shown us any of the HTML structure you want to produce or use or output, you have not told us where this key is going to come from, how many such user elements there are going to be, whether the key can be computed based on the page (post id) alone, or additional form inputs, etcetera etcetera etcetera,
There is no HTML structure to be produced. I simply need to have a link in a WordPress web site cause the execution of a plug-in of my own design. The author of the WordPress web site knows how to install the link and my plug-in, but I am responsible for what the link does!
The key will be generated and encrypted by the plug-in code (JavaScript is preferred but PHP can be used as well).
There are several things that need to be included in the key string before encryption: 1) a one-time password (I know how to do this), 2) an identification of the WordPress web site user that clicked the link (I don’t know how to get this information). Your reply is the first time I’ve heard the term “post ID” and I will research to find out what it means.
There are no other inputs, form or otherwise, just the creation of a URL with an encrypted key string – that’s it!!!
Said another way – I would like to have WP automatically and securely redirect the WP user to a ‘different, premium content, non-WP site’ when the user clicks a link to an iframe element inside of WP. The login information would be transmitted by an encoded key value generated on the WP server, and passed to the premium content site as an encoded ascii key value in the initial URL.
I will certainly post to this thread all of the information that contributes to the solution to this problem when it becomes clear to me.
I thank you for your patience!
Hey, no I’m the one who is sorry. I just find it hard to find my real voice from time to time and I say things that may be a bit too… ghastly. I was already writing an apology actually but I lost my ‘voice’ again midway… :-/.
I was meaning to write that of course you are deserving of aid, you must realize that people’s time is also limited and it could certainly be helpful (and I think it would) if you were to make sure that people here know what you are asking of them so they can properly evaluate whether to put time into it.
There is no HTML structure to be produced. I simply need to have a link in a WordPress web site cause the execution of a plug-in of my own design. The author of the WordPress web site knows how to install the link and my plug-in, but I am responsible for what the link does!
This makes no sense at all. You are either responsible or not resonsible. If you are going to be designing this site (template) then do it, otherwise don’t. If that site owner is developing his own site, then how can you be responsible? I don’t get that. Where lies your responsibility? It’s not your site? so what service are you providing to that site owner? What is your role exactly? Are you clear on that? You make it sound as if you don’t have a freakin’ clue as to what you need to be doing, like if I was asked to “insert the semophlange into the oscillating power unit entrance gateway” and I have no idea what a semophlange really is, nor where I can find that gateway whatever, but I still feel I need to do it!!
Said another way – I would like to have WP automatically and securely redirect the WP user to a ‘different, premium content, non-WP site’ when the user clicks a link to an iframe element inside of WP. The login information would be transmitted by an encoded key value generated on the WP server, and passed to the premium content site as an encoded ascii key value in the initial URL.
See, if you had started out with this, it would have made everything infinitely more understandable and clearer.
But please do define “link to an iframe element inside of WP”. An iframe loads a secondary url. I am assuming for the moment that this secondary html is generated by the same site that outputs the page itself. So what you would have is a little separate PHP script (much like what you would use for AJAX calls) that generates the link (and possibly surrounding content).
Meaning, something like
<iframe src="my-generator-script.php" />
Now you can either pass the generator script the variables you need to pass, or give it access to the WP environment. I am assuming for the moment that you don’t mean to load that premium content inside that iFrame, but rather want a complete browser-window redirect?
Not that it matters a whole lot. See, this is just yet another example of someone trying to invent the solution when he is not clear on the problem. You cannot actually invent solutions. Solutions are embedded in problems. So when you are not in touch with the problem, the solution will evade you. But if you can communicate with the problem and ask what it wants, it will ‘tell’ you the solution. So there is no ‘inventing’ or ‘making up’ involved. But you were doing that.
If the generator script loads the WP environment it will use the same cookies as the logged-in user so you will be able to get / obtain the current logged in user, just fine.
In either case, obtaining the user(login-name) is as simple as:
$login = wp_get_current_user()->user_login
There is some fuss in the help files (source files) about the “current user” possibly not being the “signed in user” but I think that is only relevant/applicable if you use a different user account solution (it is defined in pluggable.php which allows for function overrides). In general the current user should (ought to?) be the signed in user.
If there is no signed in user, the output of that line of code will produce and empty string or a null value, not sure. Actually, it is a boolean “false”. Which produces an empty string when used as a string.
However, you were talking of “redirects”. Currently you are just describing plain links. Once you have generated the encrypted key, and placed it in the HTML document OR the iframe HTML document, normally only the logged-in user would have access to that key. If you are going to use symmetric key encryption (as opposed to just e.g. base64 encoding) you cannot do the key generation in JavaScript anyway. JavaScript would need to have access to the key, pretty much voiding the security of the design as far as I’m concerned.
So either you generate the key as part of the template that outputs the HTML with the link, or you let the template include a distinct PHP output through either iframe or ajax.
That’s all there is to it? It really makes no sense to have the PHP output JavaScript or otherwise embedded parameters that the JavaScript can use for encoding a key that the PHP is in a better position to generate anyway…. given that the keys are not dynamic from the viewpoint of the browser.
Otherwise, if I take your words literally, you want the link to just be output by the template code but the link has the iframe as target.
I take it you know how to do that but here is a little idea:
$( #linkid ).on("click", function(e) { $( #iframeid ).load( e.target.href ); }
Not sure if that works, not going to test it either.
One last thing to note: the output of
wp_get_current_user()
is not really well defined in case of a non-logged-in-visitor but by default it sets the user to a non-existing user “0”. To be safe you would need to useif ( is_user_logged_in() ) : $current_user = wp_get_current_user(); $user_login = $current_user->user_login; }
That’s all I can say right now. Good luck with it.
That code is all buggy
$( #linkid ).on("click", function(e) { $( #iframeid ).load( e.target.href ); });
if (is_user_logged_in()) { $current_user = wp_get_current_user(); $user_login = $current_user->user_login; }
The element ID probably also needs quotation marks you can see how experienced I am with jQuery lol. I’m also rather sick body is forcing me to stop working so not as observant as I’d usually be..
How I’d love to have some girl with me taking care of me for a bit ;-).
But… it’s solo practice all the way.
Can’t work for the life of me and yet I am forcing myself to do some writing and video recording that just insisted it be done.
Anyay, time to sleep, hopefully.
Later…
- The topic ‘How to invoke a URL from a PHP plug-in?’ is closed to new replies.