Created
October 3, 2015 01:43
-
-
Save hideokamoto/b78d797919cb4869553e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action('admin_menu', 'sparql_hooks'); | |
add_action('save_post', 'save_sparql'); | |
add_action('wp_footer','insert_custom_js'); | |
function get_sparql_data(){ | |
$sparql = get_post_meta(get_the_ID(), '_sparql', true); | |
$sparql = urlencode($sparql); | |
$sparql_url = "http://ja.dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fja.dbpedia.org&query={$sparql}&format=application%2Fsparql-results%2Bjson&timeout=0"; | |
return $sparql_url; | |
} | |
function sparql_hooks() { | |
add_meta_box('sparql', 'SPARQL QUERY', 'sparql_input', 'post', 'normal', 'high'); | |
add_meta_box('sparql', 'SPARQL QUERY', 'sparql_input', 'page', 'normal', 'high'); | |
} | |
function sparql_input() { | |
global $post; | |
echo '<input type="hidden" name="sparql_noncename" id="sparql_noncename" value="'.wp_create_nonce('custom-js').'" />'; | |
echo '<textarea name="sparql" id="sparql" rows="5" cols="30" style="width:100%;">'.get_post_meta($post->ID,'_sparql',true).'</textarea>'; | |
} | |
function save_sparql($post_id) { | |
if (!wp_verify_nonce($_POST['sparql_noncename'], 'custom-js')) return $post_id; | |
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id; | |
$sparql = $_POST['sparql']; | |
update_post_meta($post_id, '_sparql', $sparql); | |
} | |
function insert_custom_js() { | |
if (is_page() || is_single()) { | |
if (have_posts()) : while (have_posts()) : the_post(); | |
echo "<script type='text/javascript'> | |
jQuery(document).ready(function($){ | |
$.getJSON('" . get_sparql_data() . "',function(data) { | |
$('.lod').append( | |
'<h1>'+data.results.bindings[0].num.value+'</h1><dl><dt>名称</dt><dd>'+data.results.bindings[0].name.value+'</dd><dt>住所</dt><dd>'+data.results.bindings[0].address.value+'</dd><dt>説明</dt><dd>'+data.results.bindings[0].cont.value+'</dd></dl>' | |
); | |
}) | |
}); | |
</script>"; | |
endwhile; endif; | |
rewind_posts(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment