WordPress.org

Forums

[resolved] Need Help With PHP to Customize Password-Protected Post Notification (5 posts)

  1. SpencyB
    Member
    Posted 8 years ago #

    Note: To get a feel for what I'm talking about here check, out the way my index works (not how most of the post titles are actually off-site links). And then look at what happens when a post title links to a password-protected archive page instead.

    I'm designing a theme for freelance writers to show off their work to potential clients.

    Of course, when you sell an article, you no longer hold the copyright so posting the full text or a scan on the Web is a no-no. This is often not a problem because many publications offer their content online as well, so it's just as easy to link out to their sites. I've designed my theme to be used with the a custom field called "livelink," so when you browse through the posts, the headline (post title) links off to an outside Web site if the livelink field is filled in, if not the link defaults to the permalink.

    But not every publication has a Web site yet (believe it or not) and sometimes publications don't put every scrap of content online, either. For this reason writers want to hold an archive version of each article inside a password-protected post -- either a scanned image or the raw text of the article -- for the just-in-case.
    That way, the permalink to head to a password-protected page so we don't violate any copyrights. I can just send a a potential client the password if I want him or her to be able to see the clip on my site

    For example, an article I wrote for Rolling Stone was not posted on the Web. I have a scanned version inside the post page, but I want to display a small alert for anyone browsing my site explaining the copyright situation, which also directs them to the off-site article (if available) and prompts them for the password. Here's what I want my alert to look like.

    I generated that screenshot by running some code in The Loop. This is similar to the PHP calls I make when displaying the post titles. It renders clean and simple:

    <div class="alert2"><a href="http://cspencerbeggs.com/clips/rollingstone/" class="bluelink" title="Browse more articles published by Rolling Stone">Rolling Stone</a> published and holds the copyright on this article, which can be <a href="http://cspencerbeggs.com/clips/topics/humor/hotblasphemy/" class="bluelink" title="Hot Blashphemy">viewed live on their site</a>. A password-protected copy is retained on this site.<br /> <label><b>Password:</b></label> <input name="post_password" type="password" size="20" /> <input type="submit" name="Submit" value="Submit" /></div><br />

    On the backend (again, this is if it were The Loop), the code is a mess of PHP used to call the various variables needed to display all the link info:

    <div class="alert2"><a href="<?php bloginfo('url'); ?>/clips/<?php topcat_the_main_category_slug() ?>/" class="bluelink" title="Browse more articles published by <?php topcat_the_main_category() ?>"><?php topcat_the_main_category() ?></a> published and holds the copyright on this article, which can be <a href="<?php if (get_post_meta($post->ID,'livelink',true)) {echo get_post_meta($post->ID,'livelink',true);} else {the_permalink();}; ?>" class="bluelink<?php if (get_post_meta($post->ID,'livelink',true)) { ?> offsite<?php } ?>" <?php if (get_post_meta($post->ID,'livelink',true)) { ?>rel="external" title="Read this article on the <?php topcat_the_main_category() ?> Web site"<?php } else { ?>title="<?php the_title(); ?>"<?php } ?>>viewed live on their site</a>. A password-protected copy is retained on this site.<br /> <label><b>Password:</b></label> <input name="post_password" type="password" size="20" /> <input type="submit" name="Submit" value="Submit" /></div><br />

    <b>NOTE:</b> I'm using the invaluable Top Cat plugin to generate the part of the category link for the publication.

    Finally my question: The message for the password-protected post info is generated by the function get_the_password_form() post-template.php file in ~/wp-includes/post-template.php

    I tried to hack it, but I just end up with PHP errors. I'm not sure how to escape "s and whatnot in PHP. Can anyone point me in the right direction to getting my alert to render on the password-protected pages?

  2. SpencyB
    Member
    Posted 8 years ago #

    Well, I finally figured it out. Here's the code I used:

    function get_the_password_form() {
    global $id;
    if ( get_post_meta($id, 'livelink', true) ) {
            the_excerpt();
    	$output = '<form action="' . get_option('siteurl') . '/wp-pass.php" method="post" class="alert2">
    	<p>This article was published by <a href="' . get_option('blogurl') . '/clips/' . topcat_get_the_main_category_slug() . '" class="bluelink">'  . topcat_get_the_main_category() . '</a>, which holds the copyright. You can <a href="' . get_post_meta($id,'livelink',true) . '" class="bluelink">view it live</a> on the '  . topcat_get_the_main_category() . ' Web site. A password-protected archived copy is available on this site.</p>
    	<label>Password: <input name="post_password" type="password" size="15" /></label> <input type="submit" name="Submit" value="' . __("Submit") . '" />
            </form>
    	';
    	return $output;
    }
    else {
           the_excerpt();
    	$output = '<div class="alert2"><form action="' . get_option('siteurl') . '/wp-pass.php" method="post">
    	<p><a href="' . get_option('blogurl') . '/clips/' . topcat_get_the_main_category_slug() . '" class="bluelink">'  . topcat_get_the_main_category() . '</a> holds the copyright to this article. Unfortunatly, an online version of it is unavailable on the ' . topcat_get_the_main_category() . ' Web site . You can view an archived copy here if you have a password.</p>
    	<p><label><b>Password:</b> <input class="postpassword" name="post_password" type="password" size="15" /></label> <input type="submit" name="Submit" class"passwordsubmit" value="' . __("Submit") . '" />
             </form></p></div>';
    	return $output;
    }
    }

    That outputs two different messages. One if the custom field "livelink" is filled in, and another if it's not. Again, I am using the Topcat plugin to display a bit of text.

    I have one final question -- not that my first one got answered. Does anyone know why, despite the way the code appears in $output in get_password_form(), WordPress insets line breaks (br /) in between the label, input and sumbit button? I want my password form to look like:

    Password: [__________________] [Submit]

    instead of:

    Password:
    [__________________]
    [Submit]

  3. SpencyB
    Member
    Posted 8 years ago #

    OK, so to answer that final question (I'm kind of talking to myself here, no?) I had to hack my wp/includes/formatting.php:

    in:

    function wpautop($pee, $br = 1)
    I removed "form" and "input" from:

    $allblocks = '(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|map|area|blockquote|address|math|style|script|object|param|p|h[1-6])';

  4. EastonRoyce
    Member
    Posted 8 years ago #

    I will probably never use the code you have created here. I just thought I should write and say Congratulations on your hard work and efforts! It's good to see you were able to come to the solution you needed!

    I will however bookmark this page, and if I ever hear anyone needs such a feature as you have created here, I shall tell them all about it!

  5. JMershon
    Member
    Posted 8 years ago #

    Just so you know you are not talking to yourself completely here -

    I may have a use for this - I am just beginning to use WP and I keep finding more and more neat things that I *can* do that I never knew I would *want* to do. I have marked this for future reference.

Topic Closed

This topic has been closed to new replies.

About this Topic