Wordpress Hooks + Php question

3 replies
Hi!

First post. I am trying to make my own little plugin : ).

1) What I am trying to accomplish:
-I want the plugin to save options when you click "update" in the edit page/post in the admin area.

Here is my little code:

====
add_action('save_post', 'myplugin_save_postdata'); // <-- this doesn't work the way I want it to , as in it doesn't call the function, I think, when I click "update" or "publish"


function myplugin_save_postdata( $post_id ) {
?>
<script type="text/javascript">
alert("SAVING POST");
</script>
<?php

$mydata = $_POST['myplugin_new_field'];
add_option('my_option', $mydata, '', 'yes');
update_option('my_option', $mydata, '', 'yes');
}
====

The thing is, it doesn't work. I click update the post, and there is no javascript alert box.

2) I want to echo <img src="lalala/img.png"> without it actually showing the picture.

In other words, I want it to echo the text <img src="lalala/img.png">, and not the picture lalala/img.png.

Can anyone help me .
#hooks #php #question #wordpress
  • Profile picture of the author Ronny Kibet
    Ok I am a wordpress plugin developer, and having looked at your code... I still don't quite follow. Anyway, I went ahead and create this simple piece of code as a reference for you. This might help. Let me know if you are trying to accomplish the same thing here.

    <?php

    if(isset($_POST['data'])){

    $var = $_POST['data'];

    echo $var;
    }
    ?>
    <script type="text/javascript">
    function popup(){

    alert("Saving Options")


    }
    </script>
    <form action="test.php" method="POST">
    <input type ="text" name="data" >
    <input type="submit" name="submit" value="submit" onclick="popup()"/>
    </form>

    You can copy the code and create a test.php file and try it out. since the form action is test.php or you can name your file whatever and just replace it. try entering some code on the text area and click submit, a popup appears then the data is displayed.

    Just took some bit of thinking here. Hope my time is useful.

    Regards,
    -Ronny.
    Signature

    -Subscribe to my free premium wordpress plugin giveaway. SUBSCRIBE NOW

    {{ DiscussionBoard.errors[5383048].message }}
  • Profile picture of the author Tradeout
    Hyung123,


    You are on the right line.

    Is that all of your code or is there more?

    Do you need the java alert?
    {{ DiscussionBoard.errors[5383934].message }}
  • Profile picture of the author frenchsquared
    This function saves the data, as post meta data.

    function save_callout($post_id) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    if ( isset($_POST['callout']) ) { $callout = $_POST['callout']; };
    add_post_meta($post_id, 'callout', $callout, 'false') or update_post_meta($post_id, 'callout', $callout);
    }
    add_action('save_post', 'save_callout');



    This function makes a text area in post and page

    function header_callout(){
    $post_id = (isset($_GET['post']))?addslashes($_GET['post']):'';
    $callout = stripslashes(get_post_meta($post_id, 'callout', true));
    echo '<input type="text" name="callout" value="'.$callout.'" style="width:100%" /><br/>';
    echo '<small>URL to image for header.</small>';
    }

    This function adds the prevous text field to post and page
    function add_field_boxs() {
    // add_meta_box( $id, $title, $callback, $page, $context, $priority, $callback_args );
    add_meta_box('callount', 'Header Image', 'header_callout', 'page', 'side', 'high');
    add_meta_box('callount', 'Header Image', 'header_callout', 'post', 'side', 'high');
    }
    add_action('add_meta_boxes', 'add_field_boxs');
    {{ DiscussionBoard.errors[5387381].message }}

Trending Topics