Is it possible to add custom business logic to a Custom Post Admin Edit Page?

admin2025-01-07  7

I have a custom post type that has a few fields that accept amounts. Each amount is editable. I would like to display the total of these amounts in another non-editable field on the same Custom Post Admin Page.

Is this possible?

I have a custom post type that has a few fields that accept amounts. Each amount is editable. I would like to display the total of these amounts in another non-editable field on the same Custom Post Admin Page.

Is this possible?

Share Improve this question asked Sep 28, 2013 at 21:53 Lucky LukeLucky Luke 5052 gold badges6 silver badges18 bronze badges 4
  • 1 This sounds jQuery, no? – brasofilo Commented Sep 29, 2013 at 0:28
  • Its not the jQuery that bothers me, but where fo I write the Javascript for it all to show up on the Edit Custom Post Type Page? – Lucky Luke Commented Sep 29, 2013 at 1:29
  • You can easily create meta box and pull in some content to display like the total value of a bunch of other custom fields. But if they edit the other fields it won't update the total until the post is saved. – epilektric Commented Sep 29, 2013 at 2:45
  • Maybe it isnt possible because I have used plugins to create the custom post types and there isnt an option to add Javascript anywhere – Lucky Luke Commented Sep 29, 2013 at 9:32
Add a comment  | 

1 Answer 1

Reset to default 0

You can create a plugin to add JavaScript (and stylesheets) in targeted admin pages. By the description, I think the better is to inject the total amount field together with the existent fields. Or you can create a meta box to display it. And use Stack Overflow for all your jQuery needs.

<?php
/* Plugin Name: My plugin */

# Run only in /wp-admin/post.php
add_action( 'admin_print_scripts-post.php', function()
{
    global $typenow;

    // Run only for the types Posts and Movies
    if( !in_array( $typenow, array( 'post', 'movie' ) ) )
        return;

    # codex.wordpress.org/Function_Reference/wp_enqueue_script
    wp_enqueue_script( 
            'my-custom-posts', 
            plugins_url( '/my-custom-posts.js', __FILE__ ), 
            array(), // dependencies
            false, // version
            true // on footer
    );
    # codex.wordpress.org/Function_Reference/wp_localize_script
    wp_localize_script( 
        'my-custom-posts', 
        'my_vars',
        array( 'typenow' => $typenow ) 
    );
});

And the file my-custom-posts.js in the same folder as your plugin:

jQuery(document).ready(function($) 
{    
    alert( my_vars.typenow );
    // do your stuff
});
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736264969a1009.html

最新回复(0)