0

I want to use a URL to display content in the frontend. For example

  • domain.tld/?myplugin&confirmey=KEY&mail=MAIL

But i don´t know the way(s?) to do it. For example i have a user that clicks on a confirm link in an E-Mail and i want to show him a page with some text.

  • Must exist a real page in WordPres?
  • Can i include a frontend.php in the existing Content-Area of the Theme to display my text?
  • Are there other solutions?

I don't want a user of the plugin to manually add a page or even add code to their themes functions.php

  • do you want your content to appear in the context of the current theme? – Milo Oct 16 '16 at 1:38
  • Use codex.wordpress.org/Rewrite_API/add_rewrite_rule to make any link you require. – jgraup Oct 16 '16 at 1:45
  • Yes. As an example in the layout from a post/page within the current theme. But only with my content. @Milo – wassereimer Oct 16 '16 at 1:46
  • @jgraup Do you have a small example? Isn´t the rewrite rule there to change an existing url to another? But what should my existing url be to show code in the frontend without having an existing page in WordPress? – wassereimer Oct 16 '16 at 1:59
  • 2
    this answer might help you. the problem is that theme files need a main query object, so you need a real or "virtual" page. many plugins solve this by creating pages as placeholders, then run plugin code in content filter or shortcode. – Milo Oct 16 '16 at 3:28
0

Must exist a real page in WordPres?

Yes, you need to create a page, but it doesn't have to have any content.

Let's say you created a new page named 'Confirmation message'. The URL would be http://example.com/confirmation-message.

Now add the following to the functions.php of your theme:

/**
 * Adds a confirmation message to the content of a page.
 * 
 * @param   string  $content    The current content of the page.
 * @return  string              The new content of the page.
 */
function show_confirmation_message($content) {

    // Don't add message if 'myplugin', 'confirmkey' and 'mail' are not in the URL.
    if (!isset($_GET['myplugin']) || empty($_GET['confirmkey']) || empty($_GET['mail'])) {
        return $content;
    }

    // Don't add message if we're not on a page and not in the main query.
    if( !is_page() || !is_main_query() ) {
        return $content;
    }

    // Add the logic to show a confirmaiton message here... 
    $content.= 'This is the confirmation message.';

    return $content;
}

add_filter('the_content', 'show_confirmation_message');

Now, if you go to:

http://example.com/confirmation-message?myplugin&confirmkey=KEY&mail=MAIL you should see the confirmation message.

  • I dont want a user of the plugin to manually add a page or even add code to their themes functions.php - For the own Website its a nice idea, thanks. – wassereimer Oct 16 '16 at 14:30
  • Aha. The only thing I can think of is the way that WooCommerce does it. If it detects that the page isn't there, it offers to create it for you. – Jeroen Schmit Oct 17 '16 at 10:05
0

I have found the solution I was looking for. I want to leave it here if someone else need this. With this solution the user of the Plugin don´t need to do anything (create a page or copy files).

So first we add the action to WP

add_action( 'init', 'check_url' );

Here we are looking for our URL

function check_confirm_url() {
    return false !== strpos( $_SERVER[ 'REQUEST_URI' ], '/myplugin/confirm' );
}

Here we add the content if the URL matches

function check_url() {
    if( check_confirm_url() ) {
        add_filter( 'the_posts', 'confirm_page' );
    }
}

This is the page and where we can do all the stuff

function confirm_page( $posts ) {

    //do all the stuff here

    $posts = null;
    $post = new stdClass();
    $post->post_content = "Confirm Content";
    $post->post_title = "Confirm";
    $post->post_type = "page";
    $post->comment_status = "closed";
    $posts[] = $post;
    return $posts;
}

If we now enter this URL, we have our own Page and can work with the GET data.

http://domain.tld/myplugin/confirm?mail=MAILADRESS&key=KEY

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.