2

I write a php code that read text file and it is working fine with no problem , have a look at this code.

<?php
function Read($filepath)
{
$myfile = fopen($filepath,"r") or die("Unable to open file!");
$label=fread($myfile,filesize($filepath));
fclose($myfile);
echo $label;
}
?>

now if i try to use Read function inside below input it works fine

 <input type="text" id="txtname" name="txtname" placeholder="<?php Read("resources/name_ar.txt");?>" />

I need to do the same thing using a wordpress plugin but i can't . have another look on below code

  <?php
/*
Plugin Name: my plugin
Description: my plugin
Version: 4.0
Author: me
License: GPL
*/
?>
<?php
//PHP Function to read resources files.
function Read($filepath)
{
$myfile = fopen($filepath,"r") or die("Unable to open file!");
$label=fread($myfile,filesize($filepath));
fclose($myfile);
echo $label;
}
?>

<?php 
function  form_creation()
{
    global $wpdb;
    ob_start();
?>
<form action="<?php get_permalink();?>" method="post" id="myform">
<table>
<tr>
<td>
    <h2>Asking Support</h2>
</td>
</tr>
<tr>
<td> <input type="text" id="txtname" name="txtname" placeholder="<?php Read("resources/name_ar.txt");?>" /> </td>
</tr>

</table>
</form>
<?php return ob_get_clean(); } ?>
<?php add_shortcode('myshortcode',form_creation); ?>

now when i use myshortcode nothing displayed and i think that because read function didn't be accessed , so how can Read function be accessed by form creation function

4
  • Do you have debugging enabled?
    – Milo
    Dec 28, 2014 at 17:08
  • @milo no i didn't try it , i am a WP beginner , i will read this , thanks , but do u think the code above has no problem ?
    – Bassem
    Dec 29, 2014 at 6:50
  • no, add_shortcode('myshortcode',form_creation); will give you a php warning with debugging enabled, it should be add_shortcode('myshortcode','form_creation');.
    – Milo
    Dec 29, 2014 at 16:05
  • As @Milo said, it should be add_shortcode('myshortcode','form_creation');. If the shortcode is executed, you should at least get the form, even if Read() fails. Where are you using the shortcode? It won't work in a text widget, for example.
    – Anastis
    Jul 27, 2017 at 20:02

1 Answer 1

-1

I tried creating a shortcode in my custom plugin, but in order for it to work, you need to apply a filter that converts all the new shortcodes you created to the content. Where the content is being rendered (like in the single.php file), replace the_content() with
apply_filters('the_content',get_the_content())

2
  • look , when i try to create short for one function that doesn't have any other function in its implementation (not nested function) , it worked , the problem i think in Read function
    – Bassem
    Dec 29, 2014 at 6:52
  • the_content should have filters applied to it by default, if you need to do this to make your shortcode work, you've done something wrong elsewhere.
    – Milo
    Dec 29, 2014 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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