WordPress.org

Support

Support » Plugins and Hacks » [Resolved] How do I Pass a parameter into plugin via get request

[Resolved] How do I Pass a parameter into plugin via get request

  • I’m currently migrating an existing standalone web app into a WordPress plugin. What I need to be able to do is pass a parameter into the plugin that is generated as part of a get request.

    How would I go about doing this?

    Alternatively is there another way I can pass parameters into my plugin when a user clicks a link?

Viewing 9 replies - 1 through 9 (of 9 total)
  • whooami

    @whooami

    Member

    gets are done the same way in wordpress as in any other php based scripts…

    .whoo.org/script.php?item=1

    <?php echo $_GET["item"]; ?> is going to spit out 1

    Maybe I am missing what the difficulty is that you are experiencing?

    .whoo.org/script.php?item=1
    $my_variable = $_GET["item"];

    you just set the variable 1 in whatever the plugin is..

    you will want to define that and sanitize it though further up since you are relying on user input.

    @whoami,

    Yes, your right. That will work. What I am trying to work out is how do I pass a parameter to a plugin that is running inside WordPress itself.

    – I have a plugin that is rewriting some content in a page stored with WordPress accessed via the following URL

    mydomain.com/my-page/

    What I want to do is have URLs like this so I can pass a parameter into the plugin

    mydomain.com/my-page/?my-id=10

    There are two issues here.

    A. I’m not sure how the rewrite rules would affect this, because the only reason I can use this type of URL is I have permalinks configured

    B. I’m not sure how it would work if I didn’t have permalinks running.

    With thanks

    -techxplorer

    whooami

    @whooami

    Member

    where is my-page/ is that outside of the admin area, ie its already being rewritten? If thats the case, I dont know how the get ought to to be handled or if you wouldnt be better of doing using an http_post instead.

    @whoami,

    Yes, the url to my-page/ is already being rewritten, it is a page in the WordPress system, as distinct from a post. I think I’ve worked out what I need to do.

    If I retrieve the URL of the request $_SERVER[‘REQUEST_URI’] I can determine if URLs are being rewritten. Based on my testing so far:

    – If the URL contains a ?page_id= then it is not being written and I can append my parameter onto the end and use the standard $_GET variable to get it. For example

    mydomain.com/index.php?page_id=15&my_var=10

    – If the URL does not contain ?page_id then it is being rewritten and I can append my parameter onto the end of it in a different way and still get it via the $_GET variable. For example:

    mydomain.com/my_page?my_var=10

    So far in my test both methods are working.

    Many thanks for your help.

    -techxplorer

    It looks like this works for one parameter, but not two parameters, e.g. mydomain.com/my_page?c=Crew works, but mydomain.com/my_page?c=Crew&s=Camera does not work, it results in a 404 error page. Is there a solution to this problem?

    I must admit I don’t know.

    In my initial investigation I only needed one parameter. What I’m going to do now is use AJAX to dynamically draw my content on the page instead. Which means I don’t have to worry about parameters any more.

    Sorry I can’t be of any more help.

    I need similar functionality for a page inside wordpress. I use “pretty” url’s and I can manage coding and handling e.g. http://www.mydomain.com/page?q=abcd&a=efgh links in php scripts. However, the pretty permalinks screws that up.

    techxplorer, is there a standardized way in which to do so using Ajax. I must admit I do a fait bit of php and mysql, but never came close to javascript ad ajax coding.

    might even be an option for a plugin 😉

    any help would be greatly appreciated.

    tia

    @simplr,

    At the moment I must confess that I’m not sure if there is a standardised way of doing AJAX type things in the public interface of a WordPress install.

    I do know that all the components are there, for example the ability to include various JavaScript libraries that are bundled with WordPress dynamically, including loading your own external JavaScript libraries to act as the glue between it all.

    As yet though I haven’t explored it too deeply. I’ve been working on other projects. Which interestingly enough appear to required some form of AJAX in admin pages. So in theory the same basic principles should be the same.

    My suggestion is to start by looking into either libraries made available by default via the wp_enqueue_script function.

    Sorry I can’t be of much more help at the moment

    How come the topic is marked “Resolved”?

    I’m still searching for a confirmation if
    http://my.site/2008/post-name/my-new-parameter/
    is the right way to pass my-new-parameter to post post-name.

    It seems to work in a number of combinations like
    http://my.site/2008/post-name/my-new-parameter/#comments
    or even
    http://my.site/2008/post-name/my-new-parameter/feed/

    ..but is it fool-proof?

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘[Resolved] How do I Pass a parameter into plugin via get request’ is closed to new replies.