WordPress.org

Make WordPress Core

Ticket #14930: longdesc.txt

File longdesc.txt, 2.3 KB (added by mfields, 4 years ago)
Line 
1<?php
2/*
3Plugin Name: Longdesc
4Version: 1.0.1
5Author: Michael Fields
6Author URI: http://wordpress.mfields.org/
7License: GPLv2
8
9Copyright 2010  Michael Fields  michael@mfields.org
10
11This program is free software; you can redistribute it and/or modify
12it under the terms of the GNU General Public License version 2 as published by
13the Free Software Foundation.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23
24CHANGELOG
251.0.1 - Changed action from wp_ajax_longdesc to wp_ajax_nopriv_longdesc
26
27*/
28/**
29 * Generate a html document that displays only post_content.
30 * @uses $_GET['id'] int post ID.
31 * @return void.
32 * @since 2010-09-20
33 */
34function longdesc() {
35        $id = null;
36        $content = '';
37        if( isset( $_GET['id'] ) ) {
38                $id = (int) abs( $_GET['id'] );
39        }
40        global $post;
41        $post = get_post( $id );
42        if( isset( $post->post_content ) ) {
43                remove_filter( 'the_content', 'prepend_attachment' );
44                setup_postdata( $post );
45                ?>
46<!DOCTYPE html>
47<html <?php language_attributes(); ?>>
48<head>
49<meta charset="<?php bloginfo( 'charset' ); ?>" />
50<title><?php the_title(); ?></title>
51</head>
52<body>
53<?php the_content(); ?>
54</body>
55</html>
56        <?php
57        }
58        else {
59                header( 'HTTP/1.0 404 Not Found' );
60        }
61        exit;
62}
63add_action( 'wp_ajax_longdesc', 'longdesc' );
64add_action( 'wp_ajax_nopriv_longdesc', 'longdesc' );
65/**
66 * Add longdesc attribute when WordPress sends image to the editor.
67 * @return string
68 * @since 2010-09-20
69 */
70function longdesc_add_attr( $html, $id, $caption, $title, $align, $url, $size, $alt ) {
71        $id = (int) $id;
72        $url = esc_url( admin_url( 'admin-ajax.php' ) . '?action=longdesc&amp;id=' . $id );
73        $post = get_post( $id );
74        if( isset( $post->post_content ) && !empty( $post->post_content ) ) {
75                $search = 'title="' . $title . '"';
76                $replace = $search . ' longdesc="' . $url . '"';
77                return str_replace( $search, $replace, $html );
78        }
79        return $html;
80}
81add_filter( 'image_send_to_editor', 'longdesc_add_attr', 10, 8 );
82?>