• Resolved Ivan Antipov

    (@ivantipov)


    Hi, first off the plugin is great and works perfectly. The reason why I installed it, however, was to be able to edit text boxes made on static pages under custom templates.

    I’ve read the Developer Guide, and I’m familiar with php enough to fetch/update a database, however I’m unclear on what a filter is, and how to register the new field.

    So far I have a ‘content’ table and am planning to insert various bits of content into static pages by their ID’s. Any guidance would be very useful, especially seeing as it would expand the plugin’s functionality quite drastically.

Viewing 9 replies - 1 through 9 (of 9 total)
  • For custom fields (stored in wp_postmeta) you already have the editable_post_meta() function in fields.php

    For a custom table, you only need a filter to register the new field. Please paste the code you have so far.

    Thread Starter Ivan Antipov

    (@ivantipov)

    Thanks scribu, what I meant by custom fields was a block of text to be edited on a custom template – I’m still not too familiar with WordPress terminology.

    Here’s my class:

    <?php
    
    class frontEd_custom extends frontEd_field
    {
    	protected $field;
    
    	function setup() {}
    
    	function wrap($content, $content_id = 0) {
    		if ( ! self::check($content_id) )
    			return $content;
    
    		return parent::wrap($content, $content_id);
    	}
    
    	function get($content_id) {
    		$sql = new SQL();
    		$sql->query("select * from content where id=$content_id");
    
    		if($s = $sql->fetchObject()) return $s->content;
    		else return null;
    	}
    
    	function save($content_id, $content) {
    		$sql = new SQL();
    		$sql->query("UPDATE '".DB_NAME."'.'content' SET  'content' =  '".$content."' WHERE 'content'.'id' = $content_id LIMIT 1;")
    
    		return $content;
    	}
    
    	function check($content_id)	{
    		return current_user_can('edit_post', $post->ID);
    	}
    }
    
    ?>

    On the page:

    <?php
    
    $sql = new SQL();
    $sql->query("select * from content where id = 1");
    
    if($s = $sql->fetchObject()) echo "<div class=\"front-ed\">$s->content</div>";
    
    add_action('plugins_loaded', 'register_my_field');
    function register_my_field() {
    	register_fronted_field(
    		$filter = 'the_filter',
    		$class = 'frontEd_custom',
    		$args = 'type=rich'
    	);
    }
    
    ?>

    Again, I’m clueless on what add_action and register_my_field() do, or which filter to use.

    First of all, $sql = new SQL(); is really bad, performance wise.

    You should use the $wpdb global instead.

    The developer guide was outdated. Sorry about that. It’s fixed now.

    Here is how you should display the field in your template:

    echo apply_filters('my_filter', $field_content);

    where $field_content contains the text that you want to edit.

    Then, you should put this code in functions.php:

    add_action('plugins_loaded', 'register_my_field');
    function register_my_field() {
    	register_fronted_field('my_filter', array(
    		'class' => 'frontEd_custom',
    		'title' => 'DB field',
    		'type' => 'rich'
    	));
    }

    If done right, DB field should appear on the plugin settings page.

    Also, take extra care with the check() method.

    Thread Starter Ivan Antipov

    (@ivantipov)

    Did exactly that, the text is being displayed but is not wrapped, nothing new in the FE Editor settings. Something’s missing here, i.e. how does it know which content ID is being displayed?

    Do you think you could implement something similar on your blog, I think a lot of folks would find this very useful.

    But why do you need a custom table in the first place? You already have the wp_postmeta table that can store random bits of info. And it works for pages, not only for posts. No use re-inventing the wheel.

    See add_post_meta() and related functions.

    To make them editable, just replace get_post_meta() with editable_post_meta():

    editable_post_meta(get_the_ID(), $key = 'random_bit', $type = 'rich');

    Thread Starter Ivan Antipov

    (@ivantipov)

    Man this is perfect, do forgive my WordPress noobery. One last thing, when displayed, the text isn’t divided by paragraphs, is there some way I can wrap the output in wpautop() or something?

    You’ll need the development version (1.4a2).

    Then you can do:

    echo wpautop(editable_post_meta(get_the_ID(), $key = 'random_bit', $type = 'rich', $echo = false));

    Thread Starter Ivan Antipov

    (@ivantipov)

    Thanks man, I owe you a beer if you ever come to London.

    Scotm

    (@scotm)

    Moved my question to a new post.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘[Plugin: Front-end Editor] Custom fields on static pages’ is closed to new replies.