Need help with "if"

Discussion in 'PHP' started by Divvy, Jul 3, 2017.

  1. #1
    Hello guys,

    Maybe someone can help me.

    I'm using tradeexpert script in my wordpress blog.
    To use skimming traffic, I changed the following code in my homepage

    From:
    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
    PHP:
    To:
    <a href="/webmasters/out.php?s=100,75,65&u=<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
    PHP:
    Now I have installed this plugin:
    https://wordpress.org/plugins/page-links-to/

    Screenshot of the plugin:
    [​IMG]

    My idea is to use always this code in my posts:
    <a href="/webmasters/out.php?s=100,75,65&u=<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
    PHP:
    But when I use a custom URL I want to use the default code:
    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
    PHP:
    Would be great if someone can help me with this :)

    Plugin code:
    https://paste2.org/4Fa60fpe

    Thank you guys and a good week to all!
     
    Divvy, Jul 3, 2017 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Well, you'll need some way to "pick up" that you're using either a modified URL, or the standard permalink. Then you would need to make something akin to:
    
    $link = (something to distinguish which one to use, either custom or permalink) ? the_permalink() : custom_url;
    
    Code (markup):
    And then you would just use that $link-variable in the a-tag in the code you have.

    Since I dunno how the other plugin works, or what kind of interface it uses, that's about as distinctive that I can get.
     
    PoPSiCLe, Jul 3, 2017 IP
  3. Divvy

    Divvy Well-Known Member

    Messages:
    785
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #3
    Hello PoPSiCLe,

    Thank you for trying to help me.
    Unfortunately I don't have enough skills to complete your code.

    Can this file of the plugin help you complete the code? :)
    https://paste2.org/4Fa60fpe

    Thanks!!
     
    Divvy, Jul 4, 2017 IP
  4. Divvy

    Divvy Well-Known Member

    Messages:
    785
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #4
    I'm close to the solution using this code:

    
    add_filter('the_permalink', function( $url ){
        if ( get_post_meta( get_the_ID(), '_links_to', true ) === true) {
            return $url;
        } else {
            return '/webmasters/out.php?s=100,75,65&u=' . $url;
        }
    });
    PHP:
    But unfortunately all my links are appearing with /webmasters/out.php...

    Probably the issue is in this code line:
    if ( get_post_meta( get_the_ID(), '_links_to', true ) === true) {
    PHP:
    Plugin code:
    https://paste2.org/4Fa60fpe

    Any suggestions? :)

    EDIT
    ---------------------------------

    Fixed!

    Just replaced:
    if ( get_post_meta( get_the_ID(), '_links_to', true ) === true) {
    PHP:
    With:
    
    $id=get_the_ID();
        if ( get_post_meta( $id, '_links_to', true )) {
    
    PHP:
    Thank you anyway :)
     
    Last edited: Jul 6, 2017
    Divvy, Jul 6, 2017 IP
    PoPSiCLe likes this.
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    Glad you figured it out! Good work :D
     
    PoPSiCLe, Jul 8, 2017 IP