Support » Plugin: PHP code snippets (Insert PHP) » PHP Code Not Working After Migration to Snippet

  • Resolved rjtucker

    (@rjtucker)



    The PHP code below has been working with the [insert_php] method for a long time on http://dailyprayer.ca/scripture-readings

    When I migrated the code to a snippet (minus the [insert_php] and [/insert_php] of course) it did not work correctly.

    Please tell me what might be wrong.

    Thanks.
    Jim

    [insert_php]
    date_default_timezone_set('America/Vancouver');
    
    // Create the hyperlink to today's readings on the OYBO site
    $handle = fopen("http://oneyearbibleonline.com/daily-oyb/?version=ESVUK&nltdt=" . date('md') , "r");
    if ($handle)
    {
    $srchstrg_pre = 'openweeklylink\(\'';
    $srchstrg_post = "\'\)";
    // Read line by line
    while (($line = fgets($handle)) !== false)
    {
    $line = htmlspecialchars($line);
    if (preg_match( '/' . $srchstrg_pre . '.+' . $srchstrg_post . '/', $line, $match, PREG_OFFSET_CAPTURE))
    {
    $line = $match[0][0];
    $line = substr( $line, strlen( $srchstrg_pre ) -1 );
    $line = substr( $line, 0, strlen( $line ) - strlen( $srchstrg_post ) + 2 );
    echo "Open the ".date('F j')." Readings
    in the ESV Anglicized Translation" . PHP_EOL . PHP_EOL;
    // Now stop reading the file for any more links
    break;
    }
    }
    // Close the feed
    fclose($handle);
    }
    else
    {
    // error opening the file.
    }
    
    // Display today's readings as text references
    $handle = fopen("http://oneyearbibleonline.com/daily-oyb/?version=ESVUK&nltdt=" . date('md') , "r");
    $srchstrg_pre = "One Year Bible Readings";
    if ($handle)
    {
    // Read line by line
    while (($line = fgets($handle)) !== false)
    {
    $line = htmlspecialchars($line);
    if (preg_match( '/' . $srchstrg_pre . '/', $line, $match, PREG_OFFSET_CAPTURE))
    {
    $srchstrg_pre = "<strong>.*<\/strong>";
    if (preg_match( '/' . $srchstrg_pre . '/', $line, $match, PREG_OFFSET_CAPTURE))
    {
    echo "" . substr($match[0][0],14,strlen($match[0][0])-29) . "" . PHP_EOL;
    }
    $srchstrg_pre = "<br \/>.*<br \/>";
    if (preg_match( '/' . $srchstrg_pre . '/', $line, $match, PREG_OFFSET_CAPTURE))
    {
    $line = $match[0][0];
    $srchstrg_pre = "<br \/>";
    while (preg_match( '/' . $srchstrg_pre . '/', $line, $match, PREG_OFFSET_CAPTURE))
    {
    if ($match[0][1] > 0)
    {
    echo substr($line,0,$match[0][1]) . PHP_EOL;
    }
    $line = substr($line, $match[0][1] + 12);
    }
    echo PHP_EOL;
    }
    // Now stop reading the file
    break;
    }
    }
    // Close the feed
    fclose($handle);
    }
    else
    {
    // error opening the file.
    }
    
    // Create the general links to the OYBO site and podcast
    echo "For other dates or translations,
    check the ";
    echo "One Year Bible site.". PHP_EOL . PHP_EOL;
    echo "Listen to the One Year Bible ";
    echo "podcast.";
    
    [/insert_php]

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author webcraftic

    (@webcraftic)

    Hi,
    I checked your code, as a result I received this text. Is this the right result?

    Open the July 13 Readings in the ESV Anglicized Translation For other dates or translations, check the One Year Bible site. Listen to the One Year Bible podcast.

    The problem was due to the wordpress editor converting some of the quotes when I copied the code from the old tags to the new snippets. Try to copy my code below and paste it into the snippet:

    date_default_timezone_set('America/Vancouver');
    
    	// Create the hyperlink to today's readings on the OYBO site
    	$handle = fopen("http://oneyearbibleonline.com/daily-oyb/?version=ESVUK&nltdt=" . date('md'), "r");
    	if( $handle ) {
    		$srchstrg_pre = 'openweeklylink\(\'';
    		$srchstrg_post = "\'\)";
    		// Read line by line
    		while( ($line = fgets($handle)) !== false ) {
    			$line = htmlspecialchars($line);
    			if( preg_match('/' . $srchstrg_pre . '.+' . $srchstrg_post . '/', $line, $match, PREG_OFFSET_CAPTURE) ) {
    				$line = $match[0][0];
    				$line = substr($line, strlen($srchstrg_pre) - 1);
    				$line = substr($line, 0, strlen($line) - strlen($srchstrg_post) + 2);
    				echo "Open the " . date('F j') . " Readings
    in the ESV Anglicized Translation" . PHP_EOL . PHP_EOL;
    				// Now stop reading the file for any more links
    				break;
    			}
    		}
    		// Close the feed
    		fclose($handle);
    	} else {
    		// error opening the file.
    	}
    
    	// Display today's readings as text references
    	$handle = fopen("http://oneyearbibleonline.com/daily-oyb/?version=ESVUK&nltdt=" . date('md'), "r");
    	$srchstrg_pre = "One Year Bible Readings";
    	if( $handle ) {
    		// Read line by line
    		while( ($line = fgets($handle)) !== false ) {
    			$line = htmlspecialchars($line);
    			if( preg_match('/' . $srchstrg_pre . '/', $line, $match, PREG_OFFSET_CAPTURE) ) {
    				$srchstrg_pre = "<strong>.*<\/strong>";
    				if( preg_match('/' . $srchstrg_pre . '/', $line, $match, PREG_OFFSET_CAPTURE) ) {
    					echo "" . substr($match[0][0], 14, strlen($match[0][0]) - 29) . "" . PHP_EOL;
    				}
    				$srchstrg_pre = "<br \/>.*<br \/>";
    				if( preg_match('/' . $srchstrg_pre . '/', $line, $match, PREG_OFFSET_CAPTURE) ) {
    					$line = $match[0][0];
    					$srchstrg_pre = "<br \/>";
    					while( preg_match('/' . $srchstrg_pre . '/', $line, $match, PREG_OFFSET_CAPTURE) ) {
    						if( $match[0][1] > 0 ) {
    							echo substr($line, 0, $match[0][1]) . PHP_EOL;
    						}
    						$line = substr($line, $match[0][1] + 12);
    					}
    					echo PHP_EOL;
    				}
    				// Now stop reading the file
    				break;
    			}
    		}
    		// Close the feed
    		fclose($handle);
    	} else {
    		// error opening the file.
    	}
    
    	// Create the general links to the OYBO site and podcast
    	echo "For other dates or translations,
    check the ";
    	echo "One Year Bible site." . PHP_EOL . PHP_EOL;
    	echo "Listen to the One Year Bible ";
    	echo "podcast.";

    Best regards, Alex

    rjtucker

    (@rjtucker)

    Hello, Alex.

    Thank you very much for your message.

    1. Yes, the result you showed is the result I got after I converted the code into a snippet.

    2. I carefully copied your code into the snippet, but it did not change the result.

    I’ve created http://dailyprayer.ca/scripture-readings-test so I don’t mess up the real page while troubleshooting this.

    Here are the contents of http://dailyprayer.ca/scripture-readings-test:

    <img class="alignnone size-full wp-image-1119" src="http://dailyprayer.ca/wp-content/uploads/2015/07/oybo1.png" alt="oybo" width="152" height="170" />
    
    [wbcr_php_snippet id="2745"]

    Here are the contents of snippet ID 2745 that I copied and pasted from your suggestion here on this web page:

    date_default_timezone_set('America/Vancouver');
    
    	// Create the hyperlink to today's readings on the OYBO site
    	$handle = fopen("http://oneyearbibleonline.com/daily-oyb/?version=ESVUK&nltdt=" . date('md'), "r");
    	if( $handle ) {
    		$srchstrg_pre = 'openweeklylink\(\'';
    		$srchstrg_post = "\'\)";
    		// Read line by line
    		while( ($line = fgets($handle)) !== false ) {
    			$line = htmlspecialchars($line);
    			if( preg_match('/' . $srchstrg_pre . '.+' . $srchstrg_post . '/', $line, $match, PREG_OFFSET_CAPTURE) ) {
    				$line = $match[0][0];
    				$line = substr($line, strlen($srchstrg_pre) - 1);
    				$line = substr($line, 0, strlen($line) - strlen($srchstrg_post) + 2);
    				echo "Open the " . date('F j') . " Readings in the ESV Anglicized Translation" . PHP_EOL . PHP_EOL;
    				// Now stop reading the file for any more links
    				break;
    			}
    		}
    		// Close the feed
    		fclose($handle);
    	} else {
    		// error opening the file.
    	}
    
    	// Display today's readings as text references
    	$handle = fopen("http://oneyearbibleonline.com/daily-oyb/?version=ESVUK&nltdt=" . date('md'), "r");
    	$srchstrg_pre = "One Year Bible Readings";
    	if( $handle ) {
    		// Read line by line
    		while( ($line = fgets($handle)) !== false ) {
    			$line = htmlspecialchars($line);
    			if( preg_match('/' . $srchstrg_pre . '/', $line, $match, PREG_OFFSET_CAPTURE) ) {
    				$srchstrg_pre = "<strong>.*<\/strong>";
    				if( preg_match('/' . $srchstrg_pre . '/', $line, $match, PREG_OFFSET_CAPTURE) ) {
    					echo "" . substr($match[0][0], 14, strlen($match[0][0]) - 29) . "" . PHP_EOL;
    				}
    				$srchstrg_pre = "<br \/>.*<br \/>";
    				if( preg_match('/' . $srchstrg_pre . '/', $line, $match, PREG_OFFSET_CAPTURE) ) {
    					$line = $match[0][0];
    					$srchstrg_pre = "<br \/>";
    					while( preg_match('/' . $srchstrg_pre . '/', $line, $match, PREG_OFFSET_CAPTURE) ) {
    						if( $match[0][1] > 0 ) {
    							echo substr($line, 0, $match[0][1]) . PHP_EOL;
    						}
    						$line = substr($line, $match[0][1] + 12);
    					}
    					echo PHP_EOL;
    				}
    				// Now stop reading the file
    				break;
    			}
    		}
    		// Close the feed
    		fclose($handle);
    	} else {
    		// error opening the file.
    	}
    
    	// Create the general links to the OYBO site and podcast
    	echo "For other dates or translations, check the ";
    	echo "One Year Bible site." . PHP_EOL . PHP_EOL;
    	echo "Listen to the One Year Bible ";
    	echo "podcast.";

    I don’t know what you changed. It looks the same to me.

    Please advise.

    Thanks.

    Regards,
    Jim

    rjtucker

    (@rjtucker)

    Okay I think I finally figured it out. It seems to boil down to two things:

    1. Remembering to copy from Text View in the WordPress editor (not from Visual View).

    2. Avoiding the use of constants like PHP_EOL which the [insert_php] method supported but apparently the snippet method does not (yet) support.

    Alex, please add support for constants like PHP_EOL to the snippet method.

    Following these two points I have managed to get http://dailyprayer.ca/scripture-readings working with the snippet method. I have disabled the test page that I mentioned earlier.

    Thanks.

    Regards,
    Jim

    PS – I know others have said it before, but this feels like it belongs in the category of “If it ain’t broke, don’t fix it!” From my perspective there’s little if anything gained by migrating to the snippet method. The main reason I’ve done it is because eventually you’re going to remove support for the [insert_php] method. At the very least, the snippet method is the one you’ll be improving and developing in the future, so I don’t want to get stuck on the old version. Other than that I fail to see the practical benefits of the snippet method. Indeed, stubbing my toe on these conversion and compatibility gotchas seems like pain for no discernible gain. Even so, I appreciate your plugin and I thank you for it.

    • This reply was modified 4 months ago by  rjtucker.
    Plugin Author webcraftic

    (@webcraftic)

    Hi, Jim
    Yes, really PHP_EOL does not work in the WordPress editor. If this is possible, I will try to fix this problem.

    P.S. Snippets offer many advantages. For example:
    1. You can run the code globally, not just in the records
    2. The same code is not duplicated
    3. It’s easy to change the code if you have 10,000 posts and each uses a shortcode
    4. It is safer, because no one but the administrator can edit this code
    5. You can transfer your snippets to another site
    6. The code has special formatting and syntax highlighting
    7. You can disable your snippet, right away in all entries

    And in new versions there will be even more opportunities.

    Best regards, Alex

    • This reply was modified 4 months ago by  webcraftic.
Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.